0% completed
Problem Statement:
Two strings are considered similar if you can make one string look like the other using the following two operations:
- Swap any two characters.
- For example, abde -> aedb (
e
andb
swapped).
- For example, abde -> aedb (
- Replace every occurrence of one character with another, and replace the other character with the first one.
- For example, acabbb -> bcbaaa (all a's turn into b's, and all b's turn into a's)
Given two strings, word1
and word2
, return true
if they can be made similar, otherwise return false
.
Examples
Example 1:
- Input:
word1 = "aacbbc", word2 = "bbcaca"
- Expected Output:
true
- Justification: You can swap the 'a's and 'b's in
word2
to make it "aacbcb", then swap the last two characters to matchword1
.
Example 2:
- Input:
word1 = "xxyyzz", word2 = "zzxxyy"
- Expected Output:
true
- Justification: Swapping characters
'x'
with'z'
and then'z'
with'y'
inword2
will make it "xxyyzz", which matchesword1
.
Example 3:
- Input:
word1 = "aabbcc", word2 = "aabbc"
- Expected Output:
false
- Justification: The lengths of the two strings are different, so they can't be made to look the same.
Constraints:
- 1 <= word1.length, word2.length <= 10<sup>5</sup>
- word1 and word2 contain only lowercase English letters.
Solution
To solve this problem, the first step is to ensure that both strings have the same length. If they don't, they can't be made similar. Next, we need to check if both strings have the same set of characters. If one string has a character that the other doesn't, they can't be made to look the same. Lastly, we check the frequency of each character. Both strings must have the same frequency distribution of characters for them to be transformable into one another.
This approach is effective because it simplifies the problem into three main checks: length, character set, and frequency distribution. By ensuring these conditions, we can determine if one string can be transformed into the other using the allowed operations.
Step-by-step Algorithm:
-
Check Lengths:
- If the lengths of
word1
andword2
are not the same, returnfalse
because they cannot be transformed into each other.
- If the lengths of
-
Initialize Frequency Arrays and Character Sets:
- Create two integer arrays,
count1
andcount2
, each of size 26, to store the frequency of each character inword1
andword2
respectively. - Create two sets,
set1
andset2
, to store the unique characters present inword1
andword2
respectively.
- Create two integer arrays,
-
Fill Frequency Arrays and Character Sets:
- Iterate through each character in
word1
, update its corresponding frequency incount1
, and add the character toset1
. - Iterate through each character in
word2
, update its corresponding frequency incount2
, and add the character toset2
.
- Iterate through each character in
-
Sort Frequency Arrays:
- Sort
count1
andcount2
. Sorting helps to compare the character frequencies directly.
- Sort
-
Compare Frequency Arrays and Character Sets:
- Compare the sorted frequency arrays
count1
andcount2
. If they are not the same, returnfalse
. - Compare the sets
set1
andset2
. If they are not the same, returnfalse
.
- Compare the sorted frequency arrays
-
Return Result:
- If both the sorted frequency arrays and the character sets are the same, return
true
.
- If both the sorted frequency arrays and the character sets are the same, return
Algorithm Walkthrough:
Let's walk through the algorithm using the example word1 = "aacbbc"
, word2 = "bbcaca"
.
-
Check Lengths:
- Length of
word1
is 6. - Length of
word2
is 6. - Both lengths are the same, so proceed to the next step.
- Length of
-
Initialize Frequency Arrays and Character Sets:
count1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
count2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
set1 = {}
set2 = {}
-
Fill Frequency Arrays and Character Sets:
- For
word1
:- 'a':
count1[0]
becomes 1,set1
becomes {'a'} - 'a':
count1[0]
becomes 2,set1
remains {'a'} - 'c':
count1[2]
becomes 1,set1
becomes {'a', 'c'} - 'b':
count1[1]
becomes 1,set1
becomes {'a', 'b', 'c'} - 'b':
count1[1]
becomes 2,set1
remains {'a', 'b', 'c'} - 'c':
count1[2]
becomes 2,set1
remains {'a', 'b', 'c'}
- 'a':
- For
word2
:- 'b':
count2[1]
becomes 1,set2
becomes {'b'} - 'b':
count2[1]
becomes 2,set2
remains {'b'} - 'c':
count2[2]
becomes 1,set2
becomes {'b', 'c'} - 'a':
count2[0]
becomes 1,set2
becomes {'a', 'b', 'c'} - 'c':
count2[2]
becomes 2,set2
remains {'a', 'b', 'c'} - 'a':
count2[0]
becomes 2,set2
remains {'a', 'b', 'c'}
- 'b':
- For
-
Sort Frequency Arrays:
- Sorted
count1
: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2] - Sorted
count2
: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2]
- Sorted
-
Compare Frequency Arrays and Character Sets:
count1
andcount2
are equal after sorting.set1
andset2
are equal.
-
Return Result:
- Since both the sorted frequency arrays and character sets are the same, return
true
.
- Since both the sorted frequency arrays and character sets are the same, return
Code
Complexity Analysis:
Time Complexity
The time complexity of this solution is O(n) where n is the length of the input strings. This is because we need to traverse the string. The sorting operation of count
array takes constant time as its size is constant.
Space Complexity
The space complexity is O(1) because we are using fixed-size arrays (of size 26) and sets, and the extra space used does not depend on the input size.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible