-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1657.java
More file actions
53 lines (48 loc) · 1.93 KB
/
LeetCode1657.java
File metadata and controls
53 lines (48 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.Arrays;
public class LeetCode1657 {
public static void main(String[] args) {
// 输入:word1 = "abc", word2 = "bca"
// 输出:true
// 解释:2 次操作从 word1 获得 word2 。
// 执行操作 1:"abc" -> "acb"
// 执行操作 1:"acb" -> "bca"
System.out.println(new Solution1657().closeStrings("abc", "bca"));
// 输入:word1 = "a", word2 = "aa"
// 输出:false
// 解释:不管执行多少次操作,都无法从 word1 得到 word2 ,反之亦然。
System.out.println(new Solution1657().closeStrings("a", "aa"));
// 输入:word1 = "cabbba", word2 = "abbccc"
// 输出:true
// 解释:3 次操作从 word1 获得 word2 。
// 执行操作 1:"cabbba" -> "caabbb"
// 执行操作 2:"caabbb" -> "baaccc"
// 执行操作 2:"baaccc" -> "abbccc"
System.out.println(new Solution1657().closeStrings("cabbba", "abbccc"));
// 输入:word1 = "uau", word2 = "ssx"
// 输出:true
System.out.println(new Solution1657().closeStrings("uau", "ssx"));
}
}
class Solution1657 {
public boolean closeStrings(String word1, String word2) {
if (word1.length() != word2.length()) {
return false;
}
int[] counts1 = new int[26];
int[] counts2 = new int[26];
for (int i = 0; i < word1.length(); i++) {
counts1[word1.charAt(i) - 'a']++;
counts2[word2.charAt(i) - 'a']++;
}
for (int i = 0; i < counts1.length; i++) {
if (counts1[i] + counts2[i] > 0 && counts1[i] * counts2[i] == 0) {
return false;
}
}
Arrays.sort(counts1);
Arrays.sort(counts2);
// System.out.println(Arrays.toString(counts1));
// System.out.println(Arrays.toString(counts2));
return Arrays.compare(counts1, counts2) == 0;
}
}