From 10c64097b9db865b0bfad56421dd91e605efb2af Mon Sep 17 00:00:00 2001 From: SHRUTI Date: Mon, 20 Oct 2025 08:57:27 +0530 Subject: [PATCH 1/3] Add function to find anagrams in a string Implement a function to find all anagrams of a string within another string and output their starting indices. --- find_all_Anagrams_in_string.cpp | 63 +++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 find_all_Anagrams_in_string.cpp diff --git a/find_all_Anagrams_in_string.cpp b/find_all_Anagrams_in_string.cpp new file mode 100644 index 0000000..745d410 --- /dev/null +++ b/find_all_Anagrams_in_string.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +using namespace std; + +class Solution { +public: + vector findAnagrams(string s, string p) { + vector arr(26, 0); + + int m = s.length(); + int n = p.length(); + + if (n > m) return {}; + + for (char &ch : p) + arr[ch - 'a']++; + + int i = 0, j = 0; + vector result; + + while (j < m) + { + arr[s[j] - 'a']--; + + if (j - i + 1 == n) + { + if (arr == vector(26, 0)) + result.push_back(i); + + arr[s[i] - 'a']++; + i++; + } + j++; + } + + return result; + } +}; + +int main() +{ + Solution sol; + string s, p; + + cout << "Enter string s: "; + cin >> s; + + cout << "Enter pattern p: "; + cin >> p; + + vector res = sol.findAnagrams(s, p); + + cout << "Output indices: ["; + for (int i = 0; i < res.size(); i++) + { + cout << res[i]; + if (i != res.size() - 1) cout << ", "; + } + cout << "]" << endl; + + return 0; +} From 7b64a8547607d6530b080de763e31c8ea8dd1963 Mon Sep 17 00:00:00 2001 From: SjxSubham Date: Mon, 20 Oct 2025 10:32:08 +0530 Subject: [PATCH 2/3] Apply suggestion from @sourcery-ai[bot] Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> --- find_all_Anagrams_in_string.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/find_all_Anagrams_in_string.cpp b/find_all_Anagrams_in_string.cpp index 745d410..db38bf9 100644 --- a/find_all_Anagrams_in_string.cpp +++ b/find_all_Anagrams_in_string.cpp @@ -10,9 +10,17 @@ class Solution { int m = s.length(); int n = p.length(); - + + if (n == 0) { + // For empty pattern, return all possible indices (including s.length()) + vector result; + for (int i = 0; i <= m; ++i) { + result.push_back(i); + } + return result; + } if (n > m) return {}; - + for (char &ch : p) arr[ch - 'a']++; From 012df6c0f4fbf478ebbc8d6154405e28be93f835 Mon Sep 17 00:00:00 2001 From: SHRUTI Date: Wed, 22 Oct 2025 17:51:47 +0530 Subject: [PATCH 3/3] updated_code -findAnagrams function --- find_all_Anagrams_in_string.cpp | 59 ++++++--------------------------- 1 file changed, 11 insertions(+), 48 deletions(-) diff --git a/find_all_Anagrams_in_string.cpp b/find_all_Anagrams_in_string.cpp index db38bf9..779cb4c 100644 --- a/find_all_Anagrams_in_string.cpp +++ b/find_all_Anagrams_in_string.cpp @@ -1,42 +1,29 @@ -#include -#include -#include -using namespace std; - class Solution { public: - vector findAnagrams(string s, string p) { + vector findAnagrams(string s, string p) + { vector arr(26, 0); int m = s.length(); int n = p.length(); - - if (n == 0) { - // For empty pattern, return all possible indices (including s.length()) - vector result; - for (int i = 0; i <= m; ++i) { - result.push_back(i); - } - return result; - } - if (n > m) return {}; - - for (char &ch : p) - arr[ch - 'a']++; + + for(char &ch : p) + arr[ch-'a']++; int i = 0, j = 0; vector result; - while (j < m) + while(j < m) { arr[s[j] - 'a']--; - if (j - i + 1 == n) - { - if (arr == vector(26, 0)) + if(j-i+1 == n) { + if(arr == vector(26, 0)) + { result.push_back(i); + } - arr[s[i] - 'a']++; + arr[s[i]-'a']++; i++; } j++; @@ -45,27 +32,3 @@ class Solution { return result; } }; - -int main() -{ - Solution sol; - string s, p; - - cout << "Enter string s: "; - cin >> s; - - cout << "Enter pattern p: "; - cin >> p; - - vector res = sol.findAnagrams(s, p); - - cout << "Output indices: ["; - for (int i = 0; i < res.size(); i++) - { - cout << res[i]; - if (i != res.size() - 1) cout << ", "; - } - cout << "]" << endl; - - return 0; -}