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

Method checkInclusion

String/PermutationInString.py:29–74  ·  view source on GitHub ↗

:type s1: str :type s2: str :rtype: bool

(self, s1, s2)

Source from the content-addressed store, hash-verified

27"""
28class Solution(object):
29 def checkInclusion(self, s1, s2):
30 """
31 :type s1: str
32 :type s2: str
33 :rtype: bool
34 """
35 if len(s1) > len(s2):
36 return False
37
38 counts = {}
39
40 for i in s1:
41 try:
42 counts[i] += 1
43 except:
44 counts[i] = 1
45
46 pre = counts.copy()
47
48 for c in range(len(s2)):
49 i = s2[c]
50 if i in pre:
51 pre[i] -= 1
52 if not pre[i]:
53 pre.pop(i)
54
55 if not pre:
56 return True
57 else:
58 if i in counts:
59 if i != s2[c-len(s1)+sum(pre.values())]:
60 for t in s2[c-len(s1)+sum(pre.values()):c]:
61 if t == i:
62 break
63 try:
64 pre[t] += 1
65 except:
66 pre[t] = 1
67 continue
68 pre = counts.copy()
69 if i in pre:
70 pre[i] -= 1
71 if not pre[i]:
72 pre.pop(i)
73
74 return False

Callers

nothing calls this directly

Calls 1

popMethod · 0.45

Tested by

no test coverage detected