-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlgoPlan0608
154 lines (141 loc) · 4.56 KB
/
AlgoPlan0608
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#find common characters
##idea:
- create a hash table (using hashmap) for each word, and count the characters
- for each character in the first word, loop over each map to see if it is in the key set
- if it is in the key set, deduce the value by one till it is zero
- clear out each map
- when counting if one character appears in each map, count its appearance and return the count to zero after each character
class Solution {
public List<String> commonChars(String[] words) {
Map<Character, Integer> map = new HashMap();
List<Map<Character,Integer>> list = new ArrayList();
List<String> result = new ArrayList();
for (String s: words){
for (char c: s.toCharArray()){
if (map.containsKey(c)){
map.put(c, map.get(c)+1);
}else{
map.put(c,1);
}
}
list.add(map);
for (int k: map.values()){
System.out.print(k+", ");
}
System.out.print(" // ");
map = new HashMap();
}
int count = 0;
for (char c: words[0].toCharArray()){
for (Map<Character,Integer> wordmap: list){
if (wordmap.containsKey(c) && wordmap.get(c) >0){
count++;
wordmap.put(c, wordmap.get(c)-1);
}else{
}
}
if (count == words.length) result.add(String.valueOf(c));
count =0;
}
return result;
}
}
#find intersection of arrays
##find intersection of int in both arrays.
- use the smaller array to store a map of num vs count
- loop over the second array and -- when found one
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
if (nums1.length>nums2.length){
intersect(nums2,nums1);
}
Map<Integer,Integer> map = new HashMap();
for (int num: nums1){
if(map.containsKey(num)){
map.put(num,map.get(num)+1);
}else{
map.put(num,1);
}
}
int count =0;
List<Integer> alist = new ArrayList();
for (int num: nums2){
count = map.getOrDefault(num,0);
if (count >0){
alist.add(num);
map.put(num, map.get(num)-1);
}
}
int[] intersection = new int[alist.size()];
for (int k =0; k < alist.size();k++){
intersection[k] = alist.get(k);
}
return intersection;
}
}
#find intersection of two arrays (unique ones)
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
if (nums1.length > nums2.length){
intersection(nums2,nums1);
}
List<Integer> set= new ArrayList();
for (int k : nums1){
if (!set.contains(k)){
set.add(k);
}
}
Set<Integer> list = new HashSet();
for (int j : nums2){
if (set.contains(j) && !list.contains(j)){
list.add(j);
}
}
int m = 0;
int[] result = new int[list.size()];
for (int ele: list){
result[m++] = ele;
}
return result;
}
}
#count common strings in two arrays
##idea:
-use two hashmaps where the second one filter strings based on the first map
-count the number of one-occurence strings in the second map
class Solution {
public int countWords(String[] words1, String[] words2) {
//the shortest array
if(words1.length > words2.length){
countWords(words2, words1);
}
Map<String, Integer> words = new HashMap();
for (String s: words1){
if (!words.containsKey(s)){
words.put(s, 1);
}else{
words.put(s,words.get(s)+1);
}
}
Map<String, Integer> newmap = new HashMap();
for (String k: words2){
//if it only appears once in the first array
if (words.containsKey(k) && words.get(k) ==1){
//++ so the next time it hits the map,
//it means it also appears in the second one
if (newmap.containsKey(k)){
newmap.put(k, newmap.get(k)+1);
}else{
newmap.put(k,1);
}
}
}
int count = 0;
for (String k: newmap.keySet()){
if (newmap.get(k)==1){
count++;
}
}
return count;
}
}