chendanlan
chendanlan

Reputation: 23

how to make quotes a string in python

how to process quotes issue in this question?

class Solution:
    # @param strs: A list of strings
    # @return: A list of strings
    def anagrams(self, strs):
        # write your code here
        dic = {}
        result = []

        for s in strs:
            if s == '""':
                key ='""'
            else:
                key = ''.join(sorted(s))
            dic[key].add(s)
        for key, value in dic.iteritems():
            if len(value) > 1:
                result.append(value)
        return result

Input

["",""]

Expected

["",""]

Error Message

Traceback (most recent call last): File "Main.py", line 7, in ans = Solution().anagrams(strs) File "Solution.py", line 14, in anagrams dic[key].add(s) KeyError: '' EXITCODE=1

Upvotes: 1

Views: 48

Answers (1)

Barmar
Barmar

Reputation: 780984

When you write ["",""], the elements of the input array are empty strings -- the "" are not part of the string contents. So s = '""' will not match them. You need to write:

if s == '':

Upvotes: 1

Related Questions