:type s1: str :type s2: str :rtype: bool
(self, s1, s2)
| 27 | """ |
| 28 | class 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 |