1657. Determine if Two Strings Are Close Medium
1/**
2 * [1657] Determine if Two Strings Are Close
3 *
4 * Two strings are considered close if you can attain one from the other using the following operations:
5 *
6 * Operation 1: Swap any two existing characters.
7 *
8 * For example, a<u>b</u>cd<u>e</u> -> a<u>e</u>cd<u>b</u>
9 *
10 *
11 * Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character.
12 *
13 * For example, <u>aa</u>c<u>abb</u> -> <u>bb</u>c<u>baa</u> (all a's turn into b's, and all b's turn into a's)
14 *
15 *
16 *
17 * You can use the operations on either string as many times as necessary.
18 * Given two strings, word1 and word2, return true if word1 and word2 are close, and false otherwise.
19 *
20 * Example 1:
21 *
22 * Input: word1 = "abc", word2 = "bca"
23 * Output: true
24 * Explanation: You can attain word2 from word1 in 2 operations.
25 * Apply Operation 1: "a<u>bc</u>" -> "a<u>cb</u>"
26 * Apply Operation 1: "<u>a</u>c<u>b</u>" -> "<u>b</u>c<u>a</u>"
27 *
28 * Example 2:
29 *
30 * Input: word1 = "a", word2 = "aa"
31 * Output: false
32 * Explanation: It is impossible to attain word2 from word1, or vice versa, in any number of operations.
33 *
34 * Example 3:
35 *
36 * Input: word1 = "cabbba", word2 = "abbccc"
37 * Output: true
38 * Explanation: You can attain word2 from word1 in 3 operations.
39 * Apply Operation 1: "ca<u>b</u>bb<u>a</u>" -> "ca<u>a</u>bb<u>b</u>"
40 * Apply Operation 2: "<u>c</u>aa<u>bbb</u>" -> "<u>b</u>aa<u>ccc</u>"
41 * Apply Operation 2: "<u>baa</u>ccc" -> "<u>abb</u>ccc"
42 *
43 *
44 * Constraints:
45 *
46 * 1 <= word1.length, word2.length <= 10^5
47 * word1 and word2 contain only lowercase English letters.
48 *
49 */
50pub struct Solution {}
51
52// problem: https://leetcode.com/problems/determine-if-two-strings-are-close/
53// discuss: https://leetcode.com/problems/determine-if-two-strings-are-close/discuss/?currentPage=1&orderBy=most_votes&query=
54
55// submission codes start here
56
57impl Solution {
58 pub fn close_strings(word1: String, word2: String) -> bool {
59 false
60 }
61}
62
63// submission codes end
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[test]
70 fn test_1657() {
71 }
72}
73
Back
© 2025 bowen.ge All Rights Reserved.