-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathAnagramCheck.java
37 lines (34 loc) · 1.11 KB
/
AnagramCheck.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
package Problems_On_String;
import java.util.HashMap;
import java.util.Scanner;
public class AnagramCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter word1:");
String s1 = sc.nextLine();
System.out.println("enter word2:");
String s2 = sc.nextLine();
HashMap<Character, Integer> map = new HashMap<>();
// putting the elements in the hasmap
for (char c : s1.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
boolean flag = false;
// removing elements from the hashmap
for (char c : s2.toCharArray()) {
if (!map.containsKey(c)) {
flag = true;
} else {
map.put(c, map.get(c) - 1);
if (map.get(c) == 0) {
map.remove(c);
}
}
}
if (!flag && map.isEmpty()) {
System.out.println("kudos ! they are anagrams");
} else
System.out.println("they are not anagrams.");
sc.close();
}
}