MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / findAndReplacePattern

Method findAndReplacePattern

String/FindAndReplacePattern.py:44–72  ·  view source on GitHub ↗

:type words: List[str] :type pattern: str :rtype: List[str]

(self, words, pattern)

Source from the content-addressed store, hash-verified

42
43class Solution(object):
44 def findAndReplacePattern(self, words, pattern):
45 """
46 :type words: List[str]
47 :type pattern: str
48 :rtype: List[str]
49 """
50
51 def translate_pattern(word):
52 result = ['0']
53 current = '0'
54 patterns = {word[0]: '0'}
55 for i in word[1:]:
56 if patterns.get(i) is not None:
57 result.append(patterns.get(i))
58 else:
59 current = str(int(current)+1)
60 patterns[i] = current
61 result.append(current)
62
63 return ''.join(result)
64
65 x = translate_pattern(pattern)
66
67 result = []
68 for i in words:
69 if x == translate_pattern(i):
70 result.append(i)
71
72 return result

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected