File tree Expand file tree Collapse file tree 1 file changed +62
-0
lines changed
Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+
3+ Given two strings s1 and s2, write a function to return true if s2 contains
4+ the permutation of s1. In other words, one of the first string's permutations
5+ is the substring of the second string.
6+
7+
8+
9+ Example 1:
10+
11+ Input: s1 = "ab" s2 = "eidbaooo"
12+ Output: True
13+ Explanation: s2 contains one permutation of s1 ("ba").
14+
15+ */
16+
17+ // solution
18+
19+ class Solution {
20+ public:
21+
22+ bool compare (char ar1[],char ar2[])
23+ {
24+ for (int i=0 ;i<26 ;i++)
25+ {
26+ if (ar1[i]!=ar2[i])return false ;
27+ }
28+ return true ;
29+ }
30+ bool checkInclusion (string s1, string s2) {
31+ int s1len = s1.length ();
32+ int s2len = s2.length ();
33+ int flag=0 ;
34+ // corners
35+ if (s1len == 0 || s2len < s1len)return false ;
36+
37+ char letters[26 ]={0 };
38+ char stri[26 ]={0 };
39+ for (int i=0 ;i<s1len;i++)
40+ {
41+ letters[s1[i]-' a' ]++;
42+ stri[s2[i]-' a' ]++;
43+ }
44+
45+ for (int j=s1len;j<s2len;j++)
46+ {
47+ if (compare (letters,stri))
48+ {
49+ return true ;
50+ }
51+ stri[s2[j]-' a' ]++;
52+ stri[s2[j-s1len]-' a' ]--;
53+ }
54+ if (compare (letters,stri))
55+ {
56+ return true ;
57+ }
58+ return false ;
59+
60+
61+ }
62+ };
You can’t perform that action at this time.
0 commit comments