- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
Latest commit
60 lines (46 loc) · 1.43 KB
/
Copy pathMain.java
File metadata and controls
60 lines (46 loc) · 1.43 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
packagecom.acos;
importjava.util.*;
publicclassMain {
publicstaticvoidmain(String[] args) {
Strings1 = "abba";
Strings2 = "aabb";
if (fun2(s1, s2)) {
System.out.print("Yes");
}
else {
System.out.print("No");
}
}
// The idea is, we will sort the two strings and then check whether they are equal or not
publicstaticbooleanfun1(Strings1, Strings2) {
if (s1.length() != s2.length()) {
returnfalse;
}
// We will make a character array, and then we will sort the array and change our s1 to that modified string.
// Sorting the first string
char []a1 = s1.toCharArray();
Arrays.sort(a1);
s1 = newString(a1);
// Sorting the second string
char []a2 = s2.toCharArray();
Arrays.sort(a2);
s2 = newString(a2);
returns1.equals(s2);
}
publicstaticbooleanfun2(Strings1, Strings2) {
if (s1.length() != s2.length()) {
returnfalse;
}
int []count = newint[256];
for (inti = 0; i < s1.length(); i++) {
count[s1.charAt(i)]++;
count[s2.charAt(i)]--;
}
for (inti = 0; i < 256; i++) {
if (count[i] != 0) {
returnfalse;
}
}
returntrue;
}
}