Class Solution

java.lang.Object
g0801_0900.s0854_k_similar_strings.Solution

public class Solution extends Object
854 - K-Similar Strings.<p>Hard</p> <p>Strings <code>s1</code> and <code>s2</code> are <code>k</code> <strong>-similar</strong> (for some non-negative integer <code>k</code>) if we can swap the positions of two letters in <code>s1</code> exactly <code>k</code> times so that the resulting string equals <code>s2</code>.</p> <p>Given two anagrams <code>s1</code> and <code>s2</code>, return the smallest <code>k</code> for which <code>s1</code> and <code>s2</code> are <code>k</code> <strong>-similar</strong>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s1 = &ldquo;ab&rdquo;, s2 = &ldquo;ba&rdquo;</p> <p><strong>Output:</strong> 1</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s1 = &ldquo;abc&rdquo;, s2 = &ldquo;bca&rdquo;</p> <p><strong>Output:</strong> 2</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s1.length <= 20</code></li> <li><code>s2.length == s1.length</code></li> <li><code>s1</code> and <code>s2</code> contain only lowercase letters from the set <code>{'a', 'b', 'c', 'd', 'e', 'f'}</code>.</li> <li><code>s2</code> is an anagram of <code>s1</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • kSimilarity

      public int kSimilarity(String a, String b)