Notice
Recent Posts
Recent Comments
Today
Total
01-10 18:14
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[LEETCODE] 1790. Check if One String Swap Can Make Strings Equal ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[LEETCODE] 1790. Check if One String Swap Can Make Strings Equal

WonderJay 2022. 10. 13. 16:23
728x90
๋ฐ˜์‘ํ˜•
SMALL

https://leetcode.com/

TITLE : 1790. Check if One String Swap Can Make Strings Equal

Description : You are given two strings s1 and s2 of equal length. A string swap is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices.

Return true if it is possible to make both strings equal by performing at most one string swap on exactly one of the strings. Otherwise, return false.

 

Example 1:

Input: s1 = "bank", s2 = "kanb"
Output: true
Explanation: For example, swap the first character with the last character of s2 to make "bank".

Example 2:

Input: s1 = "attack", s2 = "defend"
Output: false
Explanation: It is impossible to make them equal with one string swap.

Example 3:

Input: s1 = "kelb", s2 = "kelb"
Output: true
Explanation: The two strings are already equal, so no string swap operation is required.

 

Constraints:

  • 1 <= s1.length, s2.length <= 100
  • s1.length == s2.length
  • s1 and s2 consist of only lowercase English letters.

1. ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜ ํƒ์ƒ‰ (brute force)  || ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(N^2)

๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” ์„œ๋กœ ๊ฐ™์œผ๋ฉฐ ์ตœ๋Œ€ 100 ๋ฐ–์— ์•ˆ๋˜๋‹ค๋ณด๋‹ˆ, ๊ทธ๋ƒฅ ์ผ์ผ์ด swap ํ•ด๊ฐ€๋ฉด์„œ ๋ชจ๋“  ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ์กฐ์‚ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ตฌํ˜„ํ•˜์˜€๋‹ค. s2 ์— ๋Œ€ํ•ด์„œ swap ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜๋Š”๋ฐ, s1==s2 ์ธ ๊ฒฝ์šฐ์— true ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋„๋ก ํ•˜์˜€๋‹ค. ์ด ๋ฐฉ์‹์€ O(N^2) ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง€๋ฉฐ, N ์ด ์ตœ๋Œ€ 100 ์ด๋ฏ€๋กœ TLE ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋ผ ์˜ˆ์ƒํ•˜์˜€๊ณ , ์ œ์ถœํ•ด๋ณด๋‹ˆ ๋„‰๋„‰ํ•˜๊ฒŒ ํ†ต๊ณผํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

[C++]

// O(N^2)

class Solution {
public:
    string swap_string(string &s, int x, int y) {
        char temp_char; string temp = s;
        temp_char = temp[x];
        temp[x] = temp[y];
        temp[y] = temp_char;
        return temp;
 }

    bool areAlmostEqual(string s1, string s2) {
        if (s1 == s2) return true;
        string temp = ""; bool ans = false;
        for (int i = 0; i < s1.length()-1; i++) {
            temp.clear();
            for (int j = i+1; j < s1.length(); j++) {
                temp = swap_string(s2, i, j);
                if (temp == s1) {
                    ans = true;
                    break;
                }
            }
        }
        return ans;
    }
};

 

2. ๋ฌธ์ž์—ด ๋นˆ๋„์ˆ˜์™€ mismatch position ์˜ ๊ฐœ์ˆ˜์— ๋”ฐ๋ฅธ ํ’€์ด๋ฒ• || ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(N)

1 ๋ฒˆ ๋ฐฉ์‹์œผ๋กœ ๋ฌด์‹ํ•˜๊ฒŒ ํ‘ผ ๋’ค์—, ํ•ด๋‹น ๋ฌธ์ œ์˜ discussion page ๋ฅผ ๋‘˜๋Ÿฌ๋ณด๋ฉด์„œ ์•„๋ž˜์˜ ํ’€์ด ๋ฐฉ๋ฒ•์„ ์ฐพ์•˜๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ s1, s2 ๊ฐ€ ์„œ๋กœ AlmostEqual ์ด๋ ค๋ฉด ์•ŒํŒŒ๋ฒณ ๊ตฌ์„ฑ์ด ๋™์ผํ•˜๋‹ค๋Š” ์ „์ œ ํ•˜์— mismatched position ์˜ ๊ฐœ์ˆ˜๊ฐ€ 0 ๊ฐœ์ด๊ฑฐ๋‚˜ 2๊ฐœ์—ฌ์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด input ์ด bank, kanb ๋ผ๊ณ  ํ•˜์ž. ๋จผ์ € ๋‘ ๋ฌธ์ž์—ด์˜ ์•ŒํŒŒ๋ฒณ ๊ตฌ์„ฑ์€ ๋™์ผํ•˜๋‹ค. ๊ทธ๋ฆฌ๊ณ  mismatched position ์€ 2๊ฐœ์ด๋ฏ€๋กœ AlmostEqual ํ•˜๋‹ค. input ์ด bank, bank ๋ผ๋ฉด mismatched position ์€ 0 ๊ฐœ์ด๋ฏ€๋กœ ๋‹น์—ฐํžˆ AlmostEqual ํ•˜๋‹ค. mismatched position ์ด 0 ๊ฐœ๋„ ์•„๋‹ˆ๊ณ  2๊ฐœ๋„ ์•„๋‹ˆ๋ผ๋ฉด, 1 ๋ฒˆ์˜ swap ์œผ๋กœ ๋‘ ๋ฌธ์ž์—ด์ด ์ผ์น˜ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๋Š” ์ ˆ๋Œ€ ์—†์Œ์„ ์ง๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์šฐ๋ฆฌ๋Š” 2 ๊ฐ€์ง€๋งŒ ์ฒดํฌํ•˜๋ฉด ๋œ๋‹ค.

 

(A) ๋‘ ๋ฌธ์ž์—ด์ด ๋™์ผํ•œ ๋ฌธ์ž ๊ตฌ์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๊ฐ€?

(B) ๊ทธ๋ ‡๋‹ค๋ฉด mismatched poistion ์˜ ๊ฐœ์ˆ˜๊ฐ€ 0๊ฐœ ํ˜น์€ 2๊ฐœ์ธ๊ฐ€?

 

A ๋ฅผ ์œ„ํ•ด์„œ hashset ์„ ์ด์šฉํ•ด๋„ ๋˜๊ฒ ์ง€๋งŒ, ๊ทธ๋ƒฅ ์•ŒํŒŒ๋ฒณ ๊ฐœ์ˆ˜๋งŒํผ์˜ ๋นˆ vector ์„ ์„ ์–ธํ•ด๋†“๊ณ  ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉฐ arr[s[i]-'a' ]++, rr[s2[i]-'a']-- ๋ฅผ ์ˆ˜ํ–‰ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์„ ํƒํ•˜์˜€๋‹ค. ๋งŒ์•ฝ arr ๋ฐฐ์—ด ์ƒ์— 0์ด ์•„๋‹Œ ์›์†Œ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด ๊ณง๋ฐ”๋กœ false ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ์™œ๋ƒ๋ฉด ์•ŒํŒŒ๋ฒณ ๋นˆ๋„์ˆ˜๊ฐ€ ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์€ ์–ด๋–ป๊ฒŒ ๋ฐฐ์—ด์„ ๋ฐ”๊พธ์–ด๋„ ๋‘ ๋ฌธ์ž์—ด์ด ์ผ์น˜ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

์œ„ ๊ณผ์ •์„ ๊ฑฐ์นœ ๋’ค์—๋Š” mismatched position ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ฒ€์‚ฌํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ตœ์ข…์ ์œผ๋กœ ๊ทธ ๊ฐœ์ˆ˜๊ฐ€ 0๊ฐœ ํ˜น์€ 2๊ฐœ์ด๋ฉด true ๋ฅผ, ์•„๋‹ˆ๋ฉด false ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

[C++]

// O(N)
class Solution {
public:
    bool areAlmostEqual(string s1, string s2) {
        // check frequency of characters
        vector<int> arr(26); int cnt = 0; // # of mismatched positions
        for (int i = 0; i < s1.length(); i++) {
            arr[s1[i] - 'a']++;
            arr[s2[i] - 'a']--;
        }
        for (int i = 0; i < arr.size(); i++) {
            if (arr[i] != 0) return false;
        }
        for (int i = 0; i < s1.length(); i++) {
            if (s1[i] != s2[i]) cnt++;
        }
        return cnt == 0 || cnt == 2 ? true : false;
    }
};
728x90
๋ฐ˜์‘ํ˜•
LIST
Comments