Repository navigation
Expand file tree
/
Copy pathproblem1657.java
More file actions
87 lines (58 loc) · 1.81 KB
/
Copy pathproblem1657.java
File metadata and controls
87 lines (58 loc) · 1.81 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//1657. Determine if Two Strings Are Close
import java.util.HashMap;
import java.util.Map;
public class problem1657 {
public static boolean closeStrings(String word1, String word2) {
if(word1.length()!=word2.length())
return false;
HashMap<Character,Integer> w1 = new HashMap<>();
HashMap<Character,Integer> w2 = new HashMap<>();
for(int i=0 ;i<word1.length();i++)
{
w1.merge(word1.charAt(i),1, (a,b)->a+b);
w2.merge(word2.charAt(i),1, (a,b)->a+b);
}
for(int i=0;i<word1.length();i++)
{
if(!w1.containsKey(word2.charAt(i)))
return false;
}
HashMap<Integer,Integer> it1 = new HashMap<>();
HashMap<Integer,Integer> it2 = new HashMap<>();
for(int i : w1.values())
{
it1.merge(i, 1, (a,b)->a+b);
System.out.print(i+" ");
}
System.out.println();
for(int i : w2.values())
{
it2.merge(i, 1, (a,b)->a+b);
System.out.print(i+" ");
}
try{for(Map.Entry<Integer,Integer> i : it1.entrySet())
{
int key= i.getKey();
int value= i.getValue();
if(!(value==it2.get(key)))
return false;
}
for(Map.Entry<Integer,Integer> i : it2.entrySet())
{
int key= i.getKey();
int value= i.getValue();
if(!(value==it1.get(key)))
return false;
}}
catch(Exception e)
{
return false;
}
return true;
}
public static void main(String[] args) {
String a="abc";
String b="bca";
System.out.println(closeStrings(a, b));
}
}