-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashMap_ques.java
63 lines (53 loc) · 1.87 KB
/
hashMap_ques.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
import java.util.HashMap;
import java.util.*;
public class hashMap_ques {
public static boolean isAnangram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
HashMap<Character, Integer> list = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
list.put(ch, list.getOrDefault(ch, 0) + 1);
}
for (int i = 0; i < t.length(); i++) {
char ch = t.charAt(i);
if (list.get(ch) != null) {
if (list.get(ch) == 1) {
list.remove(ch);
} else {
list.put(ch, list.get(ch) - 1);
}
} else {
return false;
}
}
return list.isEmpty();
}
public static void main(String[] args) {
// Majority elements: time complexity = O(n)
// Given an integer array of size n, find all elements that appear more than
// [n/3] times.
int arr[] = { 1, 3, 2, 5, 1, 3, 1, 5, 1 };
HashMap<Integer, Integer> nums = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
if (nums.containsKey(arr[i])) {
nums.put(arr[i], nums.get(arr[i]) + 1);
} else {
nums.put(arr[i], 1);
}
}
for (Integer key : nums.keySet()) {
if (nums.get(key) > arr.length / 3) {
System.out.println(key);
}
}
// VALID Anagram time complexity = O(n)
// Given two strings s and t, return true if t is anagram of s else false.
// anagram = word formed by rearranging letter of a adifferent word.
// ie same number of letters and same letters used.
String s = "race";
String t = "face";
System.out.println(isAnangram(s, t));
}
}