Skip to content

Commit e462e40

Browse files
Added common-characters
1 parent 8eb426e commit e462e40

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

common_characters.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
def commonChars(self, A: List[str]) -> List[str]:
3+
4+
''' wrong approach --
5+
res, hash_ = [], dict()
6+
clist = [c for ele in A for c in ele]
7+
for ch in clist:
8+
hash_[ch] = hash_.get(ch, 0) + 1
9+
for ele, c in hash_.items():
10+
if c%len(A) == 0:
11+
res.append(ele)
12+
return res
13+
'''
14+
15+
#Naive non-pythonic code
16+
'''
17+
fin_res = []
18+
check = set(A[0])
19+
for l in check:
20+
res = min([a.count(l) for a in A])
21+
print(res)
22+
fin_res.append([l] * res)
23+
fi = [i for x in fin_res for i in x]
24+
return sorted(fi)
25+
'''
26+
#power of list comprehensions
27+
check = set(A[0])
28+
result = [[l] * min([a.count(l) for a in A]) for l in check]
29+
return sorted([i for e in result for i in e])
30+
31+

0 commit comments

Comments
 (0)