-
Notifications
You must be signed in to change notification settings - Fork 0
/
Check_Whether_Two_Strings_are_Almost_Equivalent_2068.java
58 lines (51 loc) · 1.32 KB
/
Check_Whether_Two_Strings_are_Almost_Equivalent_2068.java
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
54
55
56
57
58
package leetcode;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
class Solution2068 {
private Map<Character, Integer> w1 = new HashMap<>();
private Map<Character, Integer> w2 = new HashMap<>();
public boolean checkAlmostEquivalent(String word1, String word2) {
frequency(word1, 1);
frequency(word2, 2);
Set<Character> chars = new HashSet<>();
chars.addAll(w1.keySet());
chars.addAll(w2.keySet());
for (char c : chars) {
if (w1.containsKey(c) && w2.containsKey(c)) {
int diff = Math.abs(w1.get(c) - w2.get(c));
if (diff > 3) {
return false;
}
} else if (w1.containsKey(c) && !w2.containsKey(c)) {
if (w1.get(c) > 3) {
return false;
}
} else {
if (w2.get(c) > 3) {
return false;
}
}
}
return true;
}
public void frequency(String word, int no) {
if (no == 1) {
for (char c : word.toCharArray()) {
w1.put(c, w1.getOrDefault(c, 0) + 1);
}
} else {
for (char c : word.toCharArray()) {
w2.put(c, w2.getOrDefault(c, 0) + 1);
}
}
}
}
public class Check_Whether_Two_Strings_are_Almost_Equivalent_2068 {
public static void main(String[] args) {
Solution2068 ns = new Solution2068();
String word1 = "cccddabba", word2 = "babababab";
System.out.println(ns.checkAlmostEquivalent(word1, word2));
}
}