#854

K-Similar Strings

master · 1685 · lc hard +32 · verified · 40.6% accepted · 1,175 likes · top 20%

Description

Strings s1 and s2 are k-similar for a non-negative integer k if exactly k character swaps applied to s1 can produce s2.

Given two anagrams s1 and s2, return the minimum k for which they are k-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".

Code

1
2
3