Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue with str_diff_solution (python) #294

Open
withjak opened this issue Apr 26, 2021 · 0 comments
Open

issue with str_diff_solution (python) #294

withjak opened this issue Apr 26, 2021 · 0 comments

Comments

@withjak
Copy link

withjak commented Apr 26, 2021

`
class Solution(object):

def find_diff(self, str1, str2):
    if str1 is None or str2 is None:
        raise TypeError('str1 or str2 cannot be None')
    seen = {}
    for char in str1:
        if char in seen:
            seen[char] += 1
        else:
            seen[char] = 1
    for char in str2:
        try:
            seen[char] -= 1
        except KeyError:
            return char
        if seen[char] < 0:
            return char
    
    for char, count in seen.items():
        return char

`

Fails for input
Solution().find_diff('bbaabbc', 'aabbbb')

Last for loop should be
`
for char, count in seen.items():

if count:
    return char

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants