Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- public IList<int> FindAnagrams(string s, string p) {
- if(s.Length < p.Length){
- return new List<int>();
- }
- var maps1 = new Dictionary<char,int>();
- for(int i=0; i<p.Length;i++)
- {
- if(maps1.ContainsKey(p[i]))
- {
- maps1[p[i]]= maps1[p[i]]+1;
- }
- else {maps1[p[i]]=1;}
- }
- var res = new List<int>();
- for(int i=0; i<p.Length;i++)
- {
- if(maps1.ContainsKey(s[i]))
- {
- maps1[s[i]]--;
- }
- else {maps1[s[i]]=-1;}
- }
- var all_zero = 1;
- foreach(var n in maps1.Values)
- {
- if(n!=0){
- all_zero = 0;
- }
- }
- if(all_zero == 1){
- res.Add(0);
- }
- int k = p.Length;
- for(int i=k; i<s.Length;i++)
- {
- if(maps1.ContainsKey(s[i]))
- {
- maps1[s[i]]--;
- }
- else {maps1[s[i]]=-1;}
- if(maps1.ContainsKey(s[i-k]))
- {
- maps1[s[i-k]] = maps1[s[i-k]]+1;
- }
- else maps1[s[i-k]] = 1;
- all_zero = 1;
- foreach(var n in maps1.Values)
- {
- if(n!=0){
- all_zero = 0;
- }
- }
- if(all_zero == 1){
- res.Add(i-k+1);
- }
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement