From 7d12647260bdd0b7d28b2814b755c1f8f936122d Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Thu, 18 Jan 2024 17:29:26 +0530 Subject: [PATCH 1/4] Create Solution.cpp --- .../Solution.cpp | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/Solution.cpp diff --git a/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/Solution.cpp b/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/Solution.cpp new file mode 100644 index 0000000000000..e0e0386675cae --- /dev/null +++ b/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/Solution.cpp @@ -0,0 +1,65 @@ +class Solution { +public: + vector beautifulIndices(string s, string patternA, string patternB, int k) { + vector beautifulIndicesA = kmpSearch(s, patternA); + vector beautifulIndicesB = kmpSearch(s, patternB); + + sort(beautifulIndicesB.begin(), beautifulIndicesB.end()); + + vector result; + for (int indexA : beautifulIndicesA) { + int left = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA - k) - beautifulIndicesB.begin(); + int right = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA + k + patternB.length()) - beautifulIndicesB.begin(); + + left = (left >= 0) ? left : -(left + 1); + right = (right >= 0) ? right : -(right + 1); + + for (int indexB = left; indexB < right; indexB++) { + if (abs(beautifulIndicesB[indexB] - indexA) <= k) { + result.push_back(indexA); + break; + } + } + } + + return result; + } + +private: + vector kmpSearch(string text, string pattern) { + vector indices; + vector pi = computePrefixFunction(pattern); + + int q = 0; + for (int i = 0; i < text.length(); i++) { + while (q > 0 && pattern[q] != text[i]) { + q = pi[q - 1]; + } + if (pattern[q] == text[i]) { + q++; + } + if (q == pattern.length()) { + indices.push_back(i - q + 1); + q = pi[q - 1]; + } + } + + return indices; + } + + vector computePrefixFunction(string pattern) { + int m = pattern.length(); + vector pi(m, 0); + int k = 0; + for (int q = 1; q < m; q++) { + while (k > 0 && pattern[k] != pattern[q]) { + k = pi[k - 1]; + } + if (pattern[k] == pattern[q]) { + k++; + } + pi[q] = k; + } + return pi; + } +}; From 6c471a4f357c8d4aaeffcae8afde6838e8448b3a Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Thu, 18 Jan 2024 17:59:52 +0530 Subject: [PATCH 2/4] Update README.md --- .../README.md | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README.md b/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README.md index ed34249d3dfc7..63732b82ecebc 100644 --- a/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README.md +++ b/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README.md @@ -57,4 +57,71 @@ ## 解法 +```cpp +class Solution { +public: + vector beautifulIndices(string s, string patternA, string patternB, int k) { + vector beautifulIndicesA = kmpSearch(s, patternA); + vector beautifulIndicesB = kmpSearch(s, patternB); + + sort(beautifulIndicesB.begin(), beautifulIndicesB.end()); + + vector result; + for (int indexA : beautifulIndicesA) { + int left = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA - k) - beautifulIndicesB.begin(); + int right = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA + k + patternB.length()) - beautifulIndicesB.begin(); + + left = (left >= 0) ? left : -(left + 1); + right = (right >= 0) ? right : -(right + 1); + + for (int indexB = left; indexB < right; indexB++) { + if (abs(beautifulIndicesB[indexB] - indexA) <= k) { + result.push_back(indexA); + break; + } + } + } + + return result; + } + +private: + vector kmpSearch(string text, string pattern) { + vector indices; + vector pi = computePrefixFunction(pattern); + + int q = 0; + for (int i = 0; i < text.length(); i++) { + while (q > 0 && pattern[q] != text[i]) { + q = pi[q - 1]; + } + if (pattern[q] == text[i]) { + q++; + } + if (q == pattern.length()) { + indices.push_back(i - q + 1); + q = pi[q - 1]; + } + } + + return indices; + } + + vector computePrefixFunction(string pattern) { + int m = pattern.length(); + vector pi(m, 0); + int k = 0; + for (int q = 1; q < m; q++) { + while (k > 0 && pattern[k] != pattern[q]) { + k = pi[k - 1]; + } + if (pattern[k] == pattern[q]) { + k++; + } + pi[q] = k; + } + return pi; + } +}; +``` From 075a4442effb43b57d22e3900b65b7991317f065 Mon Sep 17 00:00:00 2001 From: Sandarbh Singhal <123533242+Nothing-avil@users.noreply.github.com> Date: Thu, 18 Jan 2024 18:00:19 +0530 Subject: [PATCH 3/4] Update README_EN.md --- .../README_EN.md | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README_EN.md b/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README_EN.md index 5d9c6473ac8ea..e636b285164f9 100644 --- a/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README_EN.md +++ b/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README_EN.md @@ -55,4 +55,72 @@ Thus we return [0] as the result. ## Solutions +```cpp +class Solution { +public: + vector beautifulIndices(string s, string patternA, string patternB, int k) { + vector beautifulIndicesA = kmpSearch(s, patternA); + vector beautifulIndicesB = kmpSearch(s, patternB); + + sort(beautifulIndicesB.begin(), beautifulIndicesB.end()); + + vector result; + for (int indexA : beautifulIndicesA) { + int left = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA - k) - beautifulIndicesB.begin(); + int right = lower_bound(beautifulIndicesB.begin(), beautifulIndicesB.end(), indexA + k + patternB.length()) - beautifulIndicesB.begin(); + + left = (left >= 0) ? left : -(left + 1); + right = (right >= 0) ? right : -(right + 1); + + for (int indexB = left; indexB < right; indexB++) { + if (abs(beautifulIndicesB[indexB] - indexA) <= k) { + result.push_back(indexA); + break; + } + } + } + + return result; + } + +private: + vector kmpSearch(string text, string pattern) { + vector indices; + vector pi = computePrefixFunction(pattern); + + int q = 0; + for (int i = 0; i < text.length(); i++) { + while (q > 0 && pattern[q] != text[i]) { + q = pi[q - 1]; + } + if (pattern[q] == text[i]) { + q++; + } + if (q == pattern.length()) { + indices.push_back(i - q + 1); + q = pi[q - 1]; + } + } + + return indices; + } + + vector computePrefixFunction(string pattern) { + int m = pattern.length(); + vector pi(m, 0); + int k = 0; + for (int q = 1; q < m; q++) { + while (k > 0 && pattern[k] != pattern[q]) { + k = pi[k - 1]; + } + if (pattern[k] == pattern[q]) { + k++; + } + pi[q] = k; + } + return pi; + } +}; +``` + From ca7e09e53a959bfa3834737aaa43bfc9b96e8ebb Mon Sep 17 00:00:00 2001 From: Nothing-avil Date: Thu, 18 Jan 2024 12:31:51 +0000 Subject: [PATCH 4/4] style: format code and docs with prettier --- .../3008.Find Beautiful Indices in the Given Array II/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README.md b/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README.md index 63732b82ecebc..bdeaf0f21d46f 100644 --- a/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README.md +++ b/solution/3000-3099/3008.Find Beautiful Indices in the Given Array II/README.md @@ -124,4 +124,5 @@ private: } }; ``` +