-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck_if_strings_equal.java
42 lines (39 loc) · 1.31 KB
/
Check_if_strings_equal.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
//leetcode - 1657. Determine if Two Strings Are Close
import java.util.HashMap;
public class Check_if_strings_equal {
public static void main(String[] args) {
String word1 = "abcaabcb";
String word2 = "bccaaabb";
System.out.println("Find if string 1 can be made using string 2 after manipulation : " + closeStrings(word1, word2));
}
public static boolean closeStrings(String word1, String word2) {
if(word1.length() != word2.length()){
return false;
}
HashMap<Character,Integer> h1 = new HashMap<>();
HashMap<Character,Integer> h2 = new HashMap<>();
for(int i =0;i<word1.length();i++){
if(!h1.containsKey(word1.charAt(i))){
h1.put(word1.charAt(i),h1.get(word1.charAt(i)+1));
}
else{
h1.put(word1.charAt(i),1);
}
}
for(int i =0;i<word2.length();i++){
if(!h2.containsKey(word2.charAt(i))){
h2.put(word2.charAt(i),h2.get(word2.charAt(i)+1));
}
else{
h2.put(word2.charAt(i),1);
}
}
if(!h1.keySet().equals(h2.keySet())){
return false;
}
if(h1.equals(h2) ){
return true;
}
return false;
}
}