1247. Minimum Swaps to Make Strings Equal Medium

@problem@discussion
#Math#String#Greedy



1/**
2 * [1247] Minimum Swaps to Make Strings Equal
3 *
4 * You are given two strings s1 and s2 of equal length consisting of letters "x" and "y" only. Your task is to make these two strings equal to each other. You can swap any two characters that belong to different strings, which means: swap s1[i] and s2[j].
5 * Return the minimum number of swaps required to make s1 and s2 equal, or return -1 if it is impossible to do so.
6 *  
7 * Example 1:
8 * 
9 * Input: s1 = "xx", s2 = "yy"
10 * Output: 1
11 * Explanation: Swap s1[0] and s2[1], s1 = "yx", s2 = "yx".
12 * 
13 * Example 2:
14 * 
15 * Input: s1 = "xy", s2 = "yx"
16 * Output: 2
17 * Explanation: Swap s1[0] and s2[0], s1 = "yy", s2 = "xx".
18 * Swap s1[0] and s2[1], s1 = "xy", s2 = "xy".
19 * Note that you cannot swap s1[0] and s1[1] to make s1 equal to "yx", cause we can only swap chars in different strings.
20 * 
21 * Example 3:
22 * 
23 * Input: s1 = "xx", s2 = "xy"
24 * Output: -1
25 * 
26 *  
27 * Constraints:
28 * 
29 * 	1 <= s1.length, s2.length <= 1000
30 * 	s1, s2 only contain 'x' or 'y'.
31 * 
32 */
33pub struct Solution {}
34
35// problem: https://leetcode.com/problems/minimum-swaps-to-make-strings-equal/
36// discuss: https://leetcode.com/problems/minimum-swaps-to-make-strings-equal/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40impl Solution {
41    pub fn minimum_swap(s1: String, s2: String) -> i32 {
42        0
43    }
44}
45
46// submission codes end
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn test_1247() {
54    }
55}
56


Back
© 2025 bowen.ge All Rights Reserved.