@@ -63,19 +63,19 @@ s: "abab" p: "ab"
6363``` python
6464class Solution :
6565 def findAnagrams (self , s : str , p : str ) -> List[int ]:
66- counter = collections. Counter(p)
67- res = []
66+ counter = Counter(p)
67+ ans = []
6868 left = right = 0
69- t = collections. Counter()
69+ t = Counter()
7070 while right < len (s):
7171 t[s[right]] += 1
7272 while t[s[right]] > counter[s[right]]:
7373 t[s[left]] -= 1
7474 left += 1
75- if right - left == len (p) - 1 :
76- res .append(left)
75+ if right - left + 1 == len (p):
76+ ans .append(left)
7777 right += 1
78- return res
78+ return ans
7979```
8080
8181### ** Java**
@@ -88,11 +88,11 @@ class Solution:
8888class Solution {
8989 public List<Integer > findAnagrams (String s , String p ) {
9090 int [] counter = new int [26 ];
91- for (int i = 0 ; i < p . length(); ++ i ) {
92- ++ counter[p . charAt(i) - ' a' ];
91+ for (char c : p . toCharArray() ) {
92+ ++ counter[c - ' a' ];
9393 }
94- List<Integer > res = new ArrayList<> ();
95- for (int i = 0 ; i <= s . length() - p . length(); ++ i) {
94+ List<Integer > ans = new ArrayList<> ();
95+ for (int i = 0 ; i + p . length() - 1 < s . length(); ++ i) {
9696 int [] t = Arrays . copyOf(counter, counter. length);
9797 boolean find = true ;
9898 for (int j = i; j < i + p. length(); ++ j) {
@@ -102,10 +102,10 @@ class Solution {
102102 }
103103 }
104104 if (find) {
105- res . add(i);
105+ ans . add(i);
106106 }
107107 }
108- return res ;
108+ return ans ;
109109 }
110110}
111111```
@@ -116,10 +116,10 @@ class Solution {
116116class Solution {
117117 public List<Integer > findAnagrams (String s , String p ) {
118118 int [] counter = new int [26 ];
119- for (int i = 0 ; i < p . length(); ++ i ) {
120- ++ counter[p . charAt(i) - ' a' ];
119+ for (char c : p . toCharArray() ) {
120+ ++ counter[c - ' a' ];
121121 }
122- List<Integer > res = new ArrayList<> ();
122+ List<Integer > ans = new ArrayList<> ();
123123 int left = 0 , right = 0 ;
124124 int [] t = new int [26 ];
125125 while (right < s. length()) {
@@ -129,16 +129,71 @@ class Solution {
129129 -- t[s. charAt(left) - ' a' ];
130130 ++ left;
131131 }
132- if (right - left == p. length() - 1 ) {
133- res . add(left);
132+ if (right - left + 1 == p. length()) {
133+ ans . add(left);
134134 }
135135 ++ right;
136136 }
137- return res ;
137+ return ans ;
138138 }
139139}
140140```
141141
142+ ### ** C++**
143+
144+ ``` cpp
145+ class Solution {
146+ public:
147+ vector<int > findAnagrams(string s, string p) {
148+ vector<int > counter(26);
149+ for (char c : p) ++counter[ c - 'a'] ;
150+ vector<int > ans;
151+ int left = 0, right = 0;
152+ vector<int > t(26);
153+ while (right < s.size())
154+ {
155+ int i = s[ right] - 'a';
156+ ++t[ i] ;
157+ while (t[ i] > counter[ i] )
158+ {
159+ --t[ s[ left] - 'a'] ;
160+ ++left;
161+ }
162+ if (right - left + 1 == p.size()) ans.push_back(left);
163+ ++right;
164+ }
165+ return ans;
166+ }
167+ };
168+ ```
169+
170+ ### **Go**
171+
172+ ```go
173+ func findAnagrams(s string, p string) []int {
174+ counter := make([]int, 26)
175+ for _, c := range p {
176+ counter[c-'a']++
177+ }
178+ var ans []int
179+ left, right := 0, 0
180+ t := make([]int, 26)
181+ for right < len(s) {
182+ i := s[right] - 'a'
183+ t[i]++
184+ for t[i] > counter[i] {
185+ t[s[left]-'a']--
186+ left++
187+ }
188+ if right-left+1 == len(p) {
189+ ans = append(ans, left)
190+ }
191+ right++
192+ }
193+ return ans
194+ }
195+ ```
196+
142197### ** ...**
143198
144199```
0 commit comments