-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidAnagram.java
52 lines (46 loc) · 2.05 KB
/
ValidAnagram.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
/*
* Given two strings s and t, return true if t is an anagram of s, and false
* otherwise.An Anagram is a word or phrase formed by rearranging the letters of
* a different word or phrase, typically using all the original letters exactly
* once.
*/
import java.util.HashMap;
class Solution1 {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
} // It is automatically false if the strings are not of the same length
// Creating a HashMap with a <key, value> pair of <Character, count> to show how
// many times a character appears in a string
HashMap<Character, Integer> countMap = new HashMap<>();
// int count = 1;
for (int i = 0; i < s.length(); i++) {
if (!countMap.containsKey(s.charAt(i))) {
countMap.put(s.charAt(i), 1);
} else {
countMap.put(s.charAt(i), countMap.get(s.charAt(i)) + 1);
}
}
for (int i = 0; i < t.length(); i++) {
char n = t.charAt(i);
if (countMap.containsKey(n) && countMap.get(n) != 0) {
if (countMap.get(n) != 0) {
countMap.put(n, countMap.get(n) - 1); /*
* Decrease the count of that character by one, once an
* instance has been encountered in string t
*/
}
} else {
return false; // If the character being iterated over is not in the existing hashmap, return
// false at that point;
}
}
return true; // If the conditions are satisfied throughout the iteration over string t
}
}
public class ValidAnagram {
public static void main(String[] args) {
Solution1 obj1 = new Solution1();
System.out.println(obj1.isAnagram("aacc", "ccac"));
}
}