MCPcopy
hub / github.com/HuberTRoy/leetCode / findAnagrams

Method findAnagrams

DP/FindAllAnagramsInAString.py:97–160  ·  view source on GitHub ↗

:type s: str :type p: str :rtype: List[int]

(self, s, p)

Source from the content-addressed store, hash-verified

95"""
96class Solution(object):
97 def findAnagrams(self, s, p):
98 """
99 :type s: str
100 :type p: str
101 :rtype: List[int]
102 """
103
104 # p = sorted(p)
105 if not s:
106 return []
107 _p = {}
108
109 for i in p:
110 try:
111 _p[i] += 1
112 except:
113 _p[i] = 1
114
115 result = []
116
117 x = _p.copy()
118 if s[0] in _p:
119 x[s[0]] -= 1
120 if not x[s[0]]:
121 x.pop(s[0])
122
123 dp = x
124 if not dp:
125 return [i for i in range(len(s)) if s[i] == p]
126
127 for i in range(1, len(s)):
128 if s[i] in dp:
129 t = dp
130 t[s[i]] -= 1
131 if not t[s[i]]:
132 t.pop(s[i])
133
134 if not t:
135 result.append(i-len(p)+1)
136 x = {}
137 _t = s[i-len(p)+1]
138 x[_t] = 1
139
140 dp = x
141 else:
142 if s[i] in _p:
143 if s[i] != s[i-len(p)+sum(dp.values())]:
144 for t in s[i-len(p)+sum(dp.values()):i]:
145 if t == s[i]:
146 break
147 try:
148 dp[t] += 1
149 except:
150 dp[t] = 1
151 continue
152
153 x = _p.copy()
154 if s[i] in x:

Callers

nothing calls this directly

Calls 1

popMethod · 0.45

Tested by

no test coverage detected