Skip to content

Commit 40fd1b3

Browse files
Create Day 18 Permutation in String.cpp
1 parent 75d69cf commit 40fd1b3

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

Day 18 Permutation in String.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
PROBLEM:
2+
3+
4+
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
5+
first string's permutations is the substring of the second string.
6+
7+
8+
Example 1:
9+
Input: s1 = "ab" s2 = "eidbaooo"
10+
Output: True
11+
Explanation: s2 contains one permutation of s1 ("ba").
12+
13+
Example 2:
14+
Input:s1= "ab" s2 = "eidboaoo"
15+
Output: False
16+
17+
Note:
18+
19+
1.The input strings only contain lower case letters.
20+
2.The length of both given strings is in range [1, 10,000].
21+
22+
23+
24+
SOLUTION:
25+
26+
27+
28+
class Solution {
29+
public:
30+
bool checkInclusion(string s1, string s2) {
31+
32+
vector<int> a(26,0); //Sliding Window
33+
vector<int> b(26,0);
34+
35+
int i,n1,n2;
36+
n1=s1.length();
37+
n2=s2.length();
38+
39+
if(n1>n2)
40+
return false;
41+
42+
for(i=0;i<n1;i++)
43+
{
44+
a[s1[i]-'a']++;
45+
b[s2[i]-'a']++;
46+
}
47+
48+
if(a==b)
49+
return true;
50+
51+
int p;
52+
p=0;
53+
for(i=n1;i<n2;i++)
54+
{
55+
b[s2[p]-'a']--;
56+
b[s2[i]-'a']++;
57+
p++;
58+
59+
if(a==b)
60+
return true;
61+
}
62+
63+
return false;
64+
65+
}
66+
};

0 commit comments

Comments
 (0)