854. K-Similar Strings Hard

@problem@discussion
#String#Breadth-First Search



1/**
2 * [854] K-Similar Strings
3 *
4 * 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.
5 * Given two anagrams s1 and s2, return the smallest k for which s1 and s2 are k-similar.
6 *  
7 * Example 1:
8 * 
9 * Input: s1 = "ab", s2 = "ba"
10 * Output: 1
11 * 
12 * Example 2:
13 * 
14 * Input: s1 = "abc", s2 = "bca"
15 * Output: 2
16 * 
17 *  
18 * Constraints:
19 * 
20 * 	1 <= s1.length <= 20
21 * 	s2.length == s1.length
22 * 	s1 and s2 contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}.
23 * 	s2 is an anagram of s1.
24 * 
25 */
26pub struct Solution {}
27
28// problem: https://leetcode.com/problems/k-similar-strings/
29// discuss: https://leetcode.com/problems/k-similar-strings/discuss/?currentPage=1&orderBy=most_votes&query=
30
31// submission codes start here
32
33impl Solution {
34    pub fn k_similarity(s1: String, s2: String) -> i32 {
35        0
36    }
37}
38
39// submission codes end
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn test_854() {
47    }
48}
49


Back
© 2025 bowen.ge All Rights Reserved.