link - LCPESY Problem - CodeChef
this as I understand it is a character counting problem, can anyone tell me why this is wrong?
# cook your dish here
for tc in range(int(input())):
str1 = list(input())
str2 = list(input())
count = 0
str1.sort()
str2.sort()
l = min(len(str1),len(str2))
for i in range(l):
if str1[i] == str2[i]:
count += 1
print(count)
Take the example of str 1 = bbc and str2 = aab. Your code will output 0, when the count is actually one. After sorting you cannot simply compare the corresponding indices. You have to try something else. In C++ I would use std::map<char, int>. I am not aware of the alternative in python.
[UPD]: Alternatively, you can use a frequency table. Declare two arrays of length 26, f1 and f2, and initialize all the elements as 0. Now traverse str1 and update the frequency of a character as f1[str1[i] - ‘a’]++. Similarly fill f2.
Now,
for i in range(26):
count += min(f1[i], f2[i]);
should do the trick.
[UPD2]: The problem seems to have case sensitive characters, so you must make a frequency table of size 52.
got it! the case you gave cleared the question for me. Thanks.