-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsortAnagram.java
More file actions
57 lines (54 loc) · 1.29 KB
/
Copy pathsortAnagram.java
File metadata and controls
57 lines (54 loc) · 1.29 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
//Sorting Approach to find if two words are anagrams
public class sortAnagram{
public static void main(String[] args) {
String s1 = "Sravani";
String s2 = "Sravani";
char[] arrayS1 = s1.toCharArray();
char[] arrayS2 = s2.toCharArray();
if(isAnagram(arrayS1, arrayS2)) {
System.out.println("the two strings are anagram of each other");
} else {
System.out.println("the two strings are not anagram of each other");
}
}
public static boolean isAnagram(char[] s1, char[] s2) {
int c1 = s1.length;
int c2 = s2.length;
if(c1 != c2) {
return false;
}
sort(s1, 0, c1 - 1);
sort(s2, 0, c2 - 1);
for(int i =0; i < c1; i++) {
if(s1.equals(s2)) {
return true;
}
}
return false;
}
public static void sort(char s[], int lo, int hi) {
if(hi <= lo)
return;
int part = partition(s, lo, hi);
sort(s, lo, part - 1);
sort(s, part + 1, hi);
}
private static int partition(char[] a, int lo, int hi) {
char x = a[hi];
int i = lo - 1;
int j;
for (j = lo; j <= hi - 1; j++) {
if(a[j] <= x) {
i++;
exchange(a[i], a[j]);
}
}
exchange(a[i+1], a[hi]);
return (i+1);
}
public static void exchange(char a, char b) {
char temp = a ;
a = b;
b = temp;
}
}