Back to course home
0% completed
Vote For New Content
Determine if Two Strings Are Close (medium)
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.
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible