-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterview03_Dictionaries_and_Hashmaps
289 lines (246 loc) · 6.42 KB
/
Interview03_Dictionaries_and_Hashmaps
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
## Interview Preparation Kit
## Dictionaries and Hashmaps
## Hash Tables: Ransom Note
def checkMagazine(magazine, note):
if len(magazine) < len(note):
return print('No')
# to build a dictionary for magazine
magazine_dict = {}
for word in magazine:
if word in magazine_dict:
magazine_dict[word] += 1
else:
magazine_dict[word] = 1
# unable to build note if the word from note is not in magazine_dict
# or if there are insufficient words in the magazine_dict
for word in note:
if word not in magazine_dict.keys() or magazine_dict[word] == 0 :
return print('No')
else:
magazine_dict[word] -= 1
print('Yes')
if __name__ == '__main__':
mn = input().split()
m = int(mn[0])
n = int(mn[1])
magazine = input().rstrip().split()
note = input().rstrip().split()
checkMagazine(magazine, note)
## Two Strings
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the twoStrings function below.
# Option 1
def twoStrings(s1, s2):
return 'YES' if any([i in s2 for i in s1]) else 'NO'
# Option 2
def twoStrings(s1, s2):
result = 'NO'
a = set(list(s1))
b = set(list(s2))
c = a.intersection(b)
if (len(c) != 0):
result = 'YES'
return result
# Option 3
def twoStrings(s1, s2):
s1 = set(s1)
s2 = set(s2)
for ele in s1:
if ele in s2:
return 'YES'
return 'NO'
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
q = int(input())
for q_itr in range(q):
s1 = input()
s2 = input()
result = twoStrings(s1, s2)
fptr.write(result + '\n')
fptr.close()
## Sherlock and Anagrams
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the sherlockAndAnagrams function below.
# Option 1
from collections import Counter
def sherlockAndAnagrams(s):
anagrams = 0
for i in range(1, len(s)):
substrings = []
for l in range(len(s)+1-i):
substrings.append(s[l:l+i])
letters_in_sub = list(map(lambda x: Counter(x), substrings))
for ele in letters_in_sub:
if letters_in_sub.count(ele)>1:
anagrams += (letters_in_sub.count(ele)-1)/2
return int(anagrams)
# Option 2
def sherlockAndAnagrams(s):
dic = {}
anagrams = 0
for k in range(1, len(s)):
for i in range(len(s)-k+1):
j = i+k
strr = ''.join(sorted(s[i:j]))
dic[strr] = dic.get(strr,0)+1
for v in dic.values():
if v>1:
anagrams += (v*(v-1))//2
return anagrams
# Option 3
def sherlockAndAnagrams(s):
anagrams = 0
sub_str_dict = {}
for pos, char in enumerate(s):
for idx in range(pos + 1, len(s) + 1):
ordered_sub_str = ''.join(sorted(s[pos:idx]))
if ordered_sub_str in sub_str_dict:
anagrams += sub_str_dict[ordered_sub_str]
sub_str_dict[ordered_sub_str] += 1
else:
sub_str_dict[ordered_sub_str] = 1
return anagrams
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
q = int(input())
for q_itr in range(q):
s = input()
result = sherlockAndAnagrams(s)
fptr.write(str(result) + '\n')
fptr.close()
## Count Triplets
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the countTriplets function below.
# Option 1
from collections import defaultdict
def countTriplets(arr, r): #(array, ratio)
c = 0 #count
triplets = defaultdict(lambda: defaultdict(lambda: 0))
for num in arr:
if num in triplets:
c += triplets[num][2]
triplets[num*r][2] += triplets[num][1]
triplets[num*r][1] += 1
return c
# Option 2
from collections import Counter
def countTriplets(arr, r):
second = Counter()
third = Counter()
c = 0 #count
for num in arr:
if num in third:
c += third[num]
if num in second:
third[num*r] += second[num]
second[num*r] += 1
return c
# Option 3
from collections import defaultdict
def countTriplets(arr, r):
second = defaultdict(int)
third = defaultdict(int)
c = 0 #count
for num in arr:
c += third[num]
third[num*r] += second[num]
second[num*r] += 1
return c
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
nr = input().rstrip().split()
n = int(nr[0])
r = int(nr[1])
arr = list(map(int, input().rstrip().split()))
ans = countTriplets(arr, r)
fptr.write(str(ans) + '\n')
fptr.close()
## Frequency Queries
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the freqQuery function below.
# Option 1
from collections import Counter
def freqQuery(queries):
result=[]
n = len(queries)
f = Counter()
ff = Counter()
for i in range(n):
o,e = queries[i]
if o==1:
ff[f[e]]-=1
f[e]+=1
ff[f[e]]+=1
elif o==2:
ff[f[e]]-=1
f[e]-=1
ff[f[e]]+=1
if f[e]<=0:
del f[e]
else:
if ff[e]>0:
result.append(1)
else:
result.append(0)
return result
# Option 2
def freqQuery(queries):
result=[]
n = len(queries)
f = {}
ff = {}
for i in range(n):
o, e = queries[i]
if (o==1):
if e not in f:
f[e]=0
ff[f[e]]=ff.setdefault(f[e],0)-1
f[e]=f.setdefault(e,0)+1
ff[f[e]]=ff.setdefault(f[e],0)+1
elif (o==2):
if e in f:
ff[f[e]]=ff.setdefault(f[e],0)-1
f[e]=f.setdefault(e,0)-1
ff[f[e]]=ff.setdefault(f[e],0)+1
if f[e]<=0:
del f[e]
else:
if e in ff:
if ff[e]:
result.append(1)
else:
result.append(0)
else:
result.append(0)
return result
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
q = int(input().strip())
queries = []
for _ in range(q):
queries.append(list(map(int, input().rstrip().split())))
ans = freqQuery(queries)
fptr.write('\n'.join(map(str, ans)))
fptr.write('\n')
fptr.close()
## end ##