Class Solution
-
- All Implemented Interfaces:
public final class Solution
854 - K-Similar Strings\.
Hard
Strings
s1
ands2
arek
\-similar (for some non-negative integerk
) if we can swap the positions of two letters ins1
exactlyk
times so that the resulting string equalss2
.Given two anagrams
s1
ands2
, return the smallestk
for whichs1
ands2
arek
\-similar.Example 1:
Input: s1 = "ab", s2 = "ba"
Output: 1
Explanation: The two string are 1-similar because we can use one swap to change s1 to s2: "ab" --> "ba".
Example 2:
Input: s1 = "abc", s2 = "bca"
Output: 2
Explanation: The two strings are 2-similar because we can use two swaps to change s1 to s2: "abc" --> "bac" --> "bca".
Constraints:
1 <= s1.length <= 20
s2.length == s1.length
s1
ands2
contain only lowercase letters from the set{'a', 'b', 'c', 'd', 'e', 'f'}
.s2
is an anagram ofs1
.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final Integer
kSimilarity(String a, String b)
-
-
Method Detail
-
kSimilarity
final Integer kSimilarity(String a, String b)
-
-
-
-