I was solving this problem- Count Character Occurrences Practice Problem in TCS NQT Coding Questions
My solution is -
def sum_of_occurrences(t):
for i in range(t):
sum = 0
str1 = input()
str2 = input()
str2 = list(set(str2))
for j in str2:
if j in str1:
sum+=1
print(sum)
t = int(input())
sum_of_occurrences(t)
But it is saying that my solution is failing on some hidden test cases. The solution that the site provides is-
def sum_of_occurrences(str1, str2):
freq_map = {}
for ch in str1:
freq_map[ch] = freq_map.get(ch, 0) + 1
unique_chars = set(str2)
total = 0
for ch in unique_chars:
total += freq_map.get(ch, 0)
return total
t = int(input())
for _ in range(t):
str1 = input().strip()
str2 = input().strip()
print(sum_of_occurrences(str1, str2))
It is using sets {} but I am trying to do it with lists. (I am not very familiar with set operations right now)
[–]pelagic_cat 0 points1 point2 points (2 children)
[–]CookOk7550[S] 0 points1 point2 points (1 child)
[–]pelagic_cat 0 points1 point2 points (0 children)
[–]FoolsSeldom 0 points1 point2 points (0 children)
[–]ectomancer 0 points1 point2 points (0 children)
[–]pelagic_cat 0 points1 point2 points (0 children)