-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram502.java
More file actions
84 lines (71 loc) · 1.77 KB
/
Copy pathprogram502.java
File metadata and controls
84 lines (71 loc) · 1.77 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
import java.util.*;
/*
* Convert str1 char array as Arr
* Convert str2 char array as Brr
*
* Create Array Count1 of size 26 for Arr
* Create Array Count2 of size 26 for Arr
*
* tarvel Arr And maintain the frequency in Count1
* tarvel Brr And maintain the frequency in Count2
*
* compare Count1 and Count 2
* if they are equal return true
* otherwise return false
*/
class Marvellous
{
public static boolean CheckAnagram(String str1, String str2)
{
boolean bflag = true;
int i = 0;
if(str1.length() != str2.length())
{
return false;
}
char Arr[] = str1.toCharArray();
char Brr[] = str2.toCharArray();
int Count[] = new int[26];
for(i = 0; i < Arr.length ;i++)
{
if(Arr[i] >= 'a' && Arr[i]<= 'z')
{
Count[Arr[i]-'a']++;
}
if(Brr[i] >= 'a' && Brr[i]<= 'z')
{
Count[Brr[i]-'a']--;
}
}
for( i = 0; i< 26 ; i++)
{
if(Count[i] != 0)
{
bflag = false;
break;
}
}
return bflag;
}
}
public class program502
{
public static void main(String[] args)
{
Scanner sobj = new Scanner(System.in);
System.out.println("Enter The 1st String : ");
String str1 = sobj.nextLine();
System.out.println("Enter The 2nd String : ");
String str2 = sobj.nextLine();
boolean bRet = false;
bRet = Marvellous.CheckAnagram(str1, str2);
if(bRet == true)
{
System.out.println("Strings are anagram");
}
else
{
System.out.println("Non Anagram");
}
}
}