forked from karterhhgg/JavaProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwentyThree.java
More file actions
51 lines (39 loc) · 1.4 KB
/
TwentyThree.java
File metadata and controls
51 lines (39 loc) · 1.4 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
// Here's the complete working code to check whether two strings are anagrams or not, considering case insensitivity, suitable for Java 7:
package HackerRank;
import java.util.Scanner;
public class TwentyThree {
static boolean isAnagram(String a, String b) {
// Complete the function
a = a.toLowerCase();
b = b.toLowerCase();
// If lengths are different, they can't be anagrams
if (a.length() != b.length()) {
return false;
}
// Use an array to count character frequencies (26 letters in English)
int[] charCount = new int[26];
// Increase count for each character in 'a'
for (int i = 0; i < a.length(); i++) {
charCount[a.charAt(i) - 'a']++;
}
// Decrease count for each character in 'b'
for (int i = 0; i < b.length(); i++) {
charCount[b.charAt(i) - 'a']--;
}
// If any count is not zero, then it's not an anagram
for (int count : charCount) {
if (count != 0) {
return false;
}
}
return true;
}
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" );
}
}