Skip to content

Practice K-Similar Strings

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#854
LeetCode Problem

K-Similar Strings

Problem Statement

Strings s1 and s2 are k-similar (for some non-negative integer k) if we can swap the positions of two letters in s1 exactly k times so that the resulting string equals s2. Given two anagrams s1 and s2, return the smallest k for which s1 and s2 are k-similar.

Source: K-Similar Strings on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s1 = "ab", s2 = "ba"
Output
1

# Example 2

Input
s1 = "abc", s2 = "bca"
Output
2

Constraints

1 <= s1.length <= 20
s2.length == s1.length
s1 and s2 contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}.
s2 is an anagram of s1.

Related LeetCode Problems

Practice more problems similar to K-Similar Strings.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.