We will find a way, we always have.

-interstellar

Problem Solving/๋ฆฌํŠธ์ฝ”๋“œ

[LeetCode] 1657. Determine if Two Strings Are Close

Redddy 2024. 1. 14. 17:29

๐Ÿ”ˆ ๋ฌธ์ œ

Two strings are considered close if you can attain one from the other using the following operations:

Operation 1: Swap any two existing characters.For example, abcde -> aecdb
Operation 2: Transform every occurrence of one existing character into another existing character, and do the same
with the other character. For example, aacabb -> bbcbaa (all a's turn into b's, and all b's turn into a's)
You can use the operations on either string as many times as necessary.
Given two strings, word1 and word2, return true if word1 and word2 are close, and false otherwise.

 

๐Ÿ’ซ ์˜ˆ์‹œ

Input: word1 = "abc", word2 = "bca"
Output: true
Explanation: You can attain word2 from word1 in 2 operations. Apply Operation 1: "abc" -> "acb" Apply Operation 1: "acb" -> "bca"

 

 

๐Ÿ“š ๋ฌธ์ œ ํ’€์ด

์ด ๋ฌธ์ œ์—์„œ ์šฐ๋ฆฌ๊ฐ€ ํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜์€ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด ์ค‘ ๋‘ ๊ฐœ์˜ ์œ„์น˜๋ฅผ ์Šค์™‘ํ•˜๋Š” ๊ฒƒ๊ณผ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด ์ค‘ ๋‘ ์ข…๋ฅ˜์˜ ๋ฌธ์ž๋ฅผ ๊ณจ๋ผ ๊ทธ ์ข…๋ฅ˜ ์ „์ฒด๋ฅผ ์Šค์™‘ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

 

์ •๋ฆฌํ•ด๋ณด์ž๋ฉด ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์—์„œ ์™„์ „ํžˆ ์ƒˆ๋กœ์šด ์•ŒํŒŒ๋ฒณ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜๋Š” ์—†๊ณ , ์•ŒํŒŒ๋ฒณ์ด ๋“ฑ์žฅํ•˜๋Š” ์ˆ˜๋“ค์˜ ๊ฐ’๋“ค์€ ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

aabbb์—์„œ ์˜คํผ๋ ˆ์ด์…˜ 2๋ฅผ ์ ์šฉํ•˜๋ฉด bbaaa๊ฐ€ ๋œ๋‹ค. 

์ž‘์—…์„ ํ•˜๊ธฐ ์ „์—๋Š” a๊ฐ€ 2๊ฐœ, b๊ฐ€ 3๊ฐœ ์žˆ์—ˆ๊ณ , ์ž‘์—… ์ ์šฉ ํ›„์—๋Š” a๊ฐ€ 3๊ฐœ, b๊ฐ€ 2๊ฐœ๊ฐ€ ๋˜์—ˆ๋‹ค. 2์™€ 3์˜ ๊ฐ’์€ ๋ณ€ํ•˜์ง€ ์•Š์•˜๊ณ , ๋ฌธ์ž์—ด์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ์•ŒํŒŒ๋ฒณ a์™€b๋„ ์—ฌ์ „ํžˆ ์กด์žฌํ•˜๊ณ  ์žˆ๋‹ค.

 

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” word1๊ณผ word2์— ์กด์žฌํ•˜๋Š” ์•ŒํŒŒ๋ฒณ์ด ๊ฐ™์€์ง€, ๊ทธ๋ฆฌ๊ณ  ๋“ฑ์žฅํ•˜๋Š” ์•ŒํŒŒ๋ฒณ ๊ฐฏ์ˆ˜๊ฐ€ ์„œ๋กœ ๋™์ผํ•œ์ง€๋ฅผ ํŒŒ์•…ํ•˜๋ฉด ๋œ๋‹ค.

 

ํŒŒ์ด์ฌ์—์„œ๋Š” Counter๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ”๋กœ ๋ฌธ์ž์—ด์—์„œ ๋ฌธ์ž๊ฐ€ ๋ช‡ ๋ฒˆ ๋“ฑ์žฅํ•˜์˜€๋Š”์ง€ ์„ธ์ค€๋‹ค. (dict ํ˜•)

 

์ฝ”๋“œํฌ์Šค์—์„œ ๋ณผ๋ฒ•ํ•œ ๋ฌธ์ œ์˜€๋‹ค.

 

๐Ÿ’ป ์ฝ”๋“œ

python

 

1
2
3
4
5
class Solution:
    def closeStrings(self, word1: str, word2: str-> bool:
        cnt1 = Counter(word1)
        cnt2 = Counter(word2)
        return cnt1.keys() == cnt2.keys() and sorted(cnt1.values()) == sorted(cnt2.values())
cs

 

 

java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
    public boolean closeStrings(String word1, String word2) {
        Map<Character, Integer> cnt1 = generateCnt(word1);
        Map<Character, Integer> cnt2 = generateCnt(word2);
        List<Integer> val1 = new ArrayList<>(cnt1.values());
        List<Integer> val2 = new ArrayList<>(cnt2.values());
        Collections.sort(val1);
        Collections.sort(val2);
        return Objects.equals(cnt1.keySet(), cnt2.keySet()) && Objects.equals(val1, val2);
    }
    
    public Map<Character, Integer> generateCnt(String word) {
        Map<Character, Integer> res = new HashMap<>();
        for (int i = 0; i < word.length(); i++) {
            int val = res.getOrDefault(word.charAt(i), 0);
            res.put(word.charAt(i), val + 1);
        }
        return res;
    }
}
cs

 

 

๐Ÿ”— ๋ฌธ์ œ๋งํฌ 

 

Determine if Two Strings Are Close - LeetCode

Can you solve this real interview question? Determine if Two Strings Are Close - Two strings are considered close if you can attain one from the other using the following operations: * Operation 1: Swap any two existing characters. * For example, abcde ->

leetcode.com

 

'Problem Solving > ๋ฆฌํŠธ์ฝ”๋“œ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[LeetCode] 3036. Number of Subarrays That Match a Pattern II  (0) 2024.02.11
[LeetCode] 380. Insert Delete GetRandom O(1)  (1) 2024.01.21