Skip to content

Latest commit

 

History

History

0854.k-similar-strings

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

题目

StringsA and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly Ktimes so that the resulting string equals B.

Given two anagrams A and B, return the smallest Kfor which A and B are K-similar.

Example 1:

Input: A = "ab", B = "ba"
Output: 1

Example 2:

Input: A = "abc", B = "bca"
Output: 2

Example 3:

Input: A = "abac", B = "baca"
Output: 2

Example 4:

Input: A = "aabc", B = "abca"
Output: 2

Note:

  1. 1 <= A.length == B.length <= 20
  2. A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}

解题思路

见程序注释