-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagram.java
More file actions
61 lines (54 loc) · 1.01 KB
/
Anagram.java
File metadata and controls
61 lines (54 loc) · 1.01 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
package poorvatutorial;
public class Anagram {
public static void AnagramAlgo(char[] a, char[] b)
{
int i =0;
int j = 0;
int ascii = 0;
int flag =1;
int[] c = new int[256];
if(a.length != b.length)
{
//System.out.println("Strings are not anagrams");
flag =0;
}
else
{
while( i < a.length)
{
ascii = (int)a[i];
c[ascii] = c[ascii]+1;
i++;
}
while( j < b.length)
{
ascii = (int)b[j];
c[ascii] = c[ascii]-1;
j++;
}
}
for(int k = 0; k < c.length; k++)
{
if(c[k] != 0)
{
flag=0;
}
}
if(flag ==1)
{
System.out.println("its a anagram");
}
else
{
System.out.println("its not a anagram");
}
}
public static void main(String[] args )
{
String s1 = "poorva";
String s2 = "hajakak";
char[] a = s1.toCharArray();
char[] b = s2.toCharArray();
AnagramAlgo(a,b);
}
}