Skip to content

Commit 14e03a4

Browse files
author
ishan.gupta
committed
permutation in string leetcode
1 parent dbef70e commit 14e03a4

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

permutation-in-string.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
using namespace std;
3+
#include <iostream>
4+
#include <bits/stdc++.h>
5+
// remove above lines when running in leetcode.
6+
7+
class Solution {
8+
public:
9+
bool checkInclusion(string s1, string s2) {
10+
int n = s1.size();
11+
int m = s2.size();
12+
if(n>m) return false;
13+
vector<int> s1_v(26,0);
14+
vector<int> s2_v(26,0);
15+
for(int i=0;i<n;i++){
16+
s1_v[s1[i]-'a']++;
17+
s2_v[s2[i]-'a']++;
18+
}
19+
if(s1_v==s2_v) return true;
20+
21+
for(int i=n;i<m;i++){
22+
s2_v[s2[i-n]-'a']--;
23+
s2_v[s2[i]-'a']++;
24+
if(s1_v==s2_v) return true;
25+
}
26+
return false;
27+
}
28+
};
29+
30+
// https://leetcode.com/problems/permutation-in-string/

0 commit comments

Comments
 (0)