[567] Permutation in String
https://leetcode.com/problems/permutation-in-string/description/
- algorithms
- Medium (36.86%)
- Source Code: 567.permutation-in-string.py
- Total Accepted: 43.5K
- Total Submissions: 114.1K
- Testcase Example: '"ab"\n"eidbaooo"'
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string's permutations is the substring of the second string.
Example 1:
Input: s1 = "ab" s2 = "eidbaooo" Output: True Explanation: s2 contains one permutation of s1 ("ba").
Example 2:
Input:s1= "ab" s2 = "eidboaoo" Output: False
Note:
The input strings only contain lower case letters.
The length of both given strings is in range [1, 10,000].
python
class Solution(object):
def checkInclusion(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: bool
"""
if not s1: return True
len1 = len(s1)
len2 = len(s2)
if len1 > len2: return False
S, V = [0]*26, [0]*26
for i in range(len1):
S[ord(s1[i])-ord('a')] += 1
V[ord(s2[i])-ord('a')] += 1
# print('S', S)
for i in range(len1-1, len2):
if S == V: return True
if i == len1-1: continue
else:
V[ord(s2[i-len1])-ord('a')] -= 1
V[ord(s2[i])-ord('a')] += 1
# print('V', V)
if S == V: return True
return False
#
# s1 = "adc"
# s2 = "dcda"
# #
#
# s = Solution()
# print s.checkInclusion(s1, s2)
# print s1, s2