-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJava_Anagrams.java
117 lines (89 loc) · 2.94 KB
/
Java_Anagrams.java
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* Two strings, and , are called anagrams if they contain all the same characters in the same frequencies. For this challenge, the test is not case-sensitive. For example, the anagrams of CAT are CAT, ACT, tac, TCA, aTC, and CtA.
Function Description
Complete the isAnagram function in the editor.
isAnagram has the following parameters:
string a: the first string
string b: the second string
Returns
boolean: If and are case-insensitive anagrams, return true. Otherwise, return false.
Input Format
The first line contains a string .
The second line contains a string .
Constraints
1<= length(a), length(b) <=50
Strings and consist of English alphabetic characters.
The comparison should NOT be case sensitive.
Sample Input 0
anagram
margana
Sample Output 0
Anagrams
Explanation 0
Character Frequency: anagram Frequency: margana
A or a 3 3
G or g 1 1
N or n 1 1
M or m 1 1
R or r 1 1
The two strings contain all the same letters in the same frequencies, so we print "Anagrams".
Sample Input 1
anagramm
marganaa
Sample Output 1
Not Anagrams
Explanation 1
Character Frequency: anagramm Frequency: marganaa
A or a 3 4
G or g 1 1
N or n 1 1
M or m 2 1
R or r 1 1
The two strings don't contain the same number of a's and m's, so we print "Not Anagrams".
Sample Input 2
Hello
hello
Sample Output 2
Anagrams
Explanation 2
Character Frequency: Hello Frequency: hello
E or e 1 1
H or h 1 1
L or l 2 2
O or o 1 1
The two strings contain all the same letters in the same frequencies, so we print "Anagrams".*/
//SOLUTION//
import java.util.Scanner;
public class Solution {
static boolean isAnagram(String a, String b) {
String s1 = a;
String s2 = b;
s1=s1.toLowerCase();
s2=s2.toLowerCase();
if(s1.length()==s2.length())
{
int[] arr = new int[256];
int[] brr = new int[256];
for (int i = 0; i < s1.length(); i++) {
arr[(int) s1.charAt(i)] += 1;
brr[(int) s2.charAt(i)] += 1;
}
for (int i = 0; i < 256; i++) {
if (arr[i] != brr[i])
return false;
}
return true;
}
else
{
return false;
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String a = scan.next();
String b = scan.next();
scan.close();
boolean ret = isAnagram(a, b);
System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
}
}