- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximize count of occurrences of S2 in S1 as a subsequence by concatenating N1 and N2 times respectively
The following article discusses an approach to count the maximum occurrences of a string s2 in another string s1 after they have been concatenated N1 and N2 times respectively. This is an interesting type of pattern searching problem. In this article we have employed a relatively intuitive solution approach.
Problem Statement
The task is to determine the maximum number of non-overlapping occurrences of string s2 within string s1. The strings are concatenated multiple times: s1 is repeated n1 times, and s2 is repeated n2 times.
Example
Input
s1 = “abc”, s2 = “ac”, n1 = 4, n2 = 2
Output
2
Explanation
After concatenating string S1 four times, we obtain the string "abcabcabcabc". Similarly, concatenating string S2 two times results in "acac". By observing this, we can ascertain that the string S2 appears twice as a non-overlapping subsequence within the string S1. Hence, the desired output is 2.
Input
s1 = “”, s2 = “sfa”, n1 = 7, n2 = 8
Output
0
Explanation
Since s1 is an empty string, upon self concatenation n1 times, it will still remain empty. Therefore there will be no occurrence of s2 concatenated n2 times in s1.
Solution Approach
A possible solution to the problem which is fairly intuitive can be that we first concatenate the strings s1 and s2 n1 and n2 times, respectively. This ensures that the strings are long enough to be found in each other. We then iterate over the characters in s1 and check if they are equal to the corresponding characters in s2. If they are,we move on to the next character in both the strings. If we reach the length of s2, we increment the count of occurrences. Finally, we return the count of occurrences. The pseudocode and C++ program provided below implement this approach.
Pseudocode
Declare variables
Repeat n1 times
Repeat n2 times
Iterate over the characters in s1
string s1 - The string to be searched
int n1 - The number of times s1 is concatenated
string s2 - The string to be searched for
int n2 - The number of times s2 is concatenated
Concatenate s1 with temp
Concatenate s2 with temp
Iterate over the characters in s2
if current character of string s1 is identical to the current character of string s2, Increment j and i Else, Increment i If j reaches the length of s2, Increment the count of occurrences
Return the count of occurrences
Example: C++ Program
The function countOccurrence() counts the number of occurrences of s2 in s1. n1 and n2 are the number of times s1 and s2 are concatenated. The function first concatenates s1 with itself n1 times and s2 with itself n2 times. Then, it iterates over s1 and s2 simultaneously, checking for matches. If a match is found, the function increments the occurrence counter. The function returns the final value of the counter. There is also a condition in which s2 is an empty string. The program returns 1 since an empty string occurs once as a subsequence.
Example
#include <iostream> #include <map> #include <string> using namespace std; // The purpose of this function is to determine the count of occurrences of string s2 within string s1. // The variables n1 and n2 represent the number of times s1 and s2 are concatenated, respectively. int countOccurrence(string s1, int n1, string s2, int n2){ string temp = s1; while (n1 > 1){ s1 += temp; n1--; } temp = s2; while (n2 > 1){ s2 += temp; n2--; } int i = 0, j = 0, count = 0; while (i < s1.length()){ j = 0; while (j < s2.length() && i < s1.length()){ // In the event that the current character of s1 matches the current character of s2, // increase both j and i by one. if (s1[i] == s2[j]){ j++; i++; } // Else move on to the next character in 's1' else{ i++; } // If `j` reaches the length of `s2`, increment the count of occurrences. if (j == s2.length()){ count++; } } } // Return the count of occurrences. return count; } int main(){ string s1 = "abac"; int n1 = 5; cout << "s1: " << s1 << " and n1: " << n1 <<endl; string s2 = "a"; int n2 = 4; cout << "s2: " << s2 << " and n2: " << n2 << endl; if (s2.length() == 0){ cout << 1 << endl; return 0; } cout << "count of occurrences of string s2 within string s1: "<< (countOccurrence(s1, n1, s2, n2)); return 0; }
Output
s1: abac and n1: 5 s2: a and n2: 4 count of occurrences of string s2 within string s1: 2
Time and Space Complexity Analysis
Time Complexity: O(NM)
N and M are the lengths of the strings s1 and s2 after being concatenated n1 and n2 times respectively.
Space Complexity: O(N)
A temp variable is used to store the string while concatenating. The length of the string temp = max(length(s1), length(s2)).
Conclusion
The article presents a method to maximize the count of occurrences of string s2 within string s1 as a subsequence. This is achieved by concatenating s1 n1 times and s2 n2 times. We discussed the problem statement with the help of suitable examples. The solution approach provided works in O(NM) time and is fairly intuitive. The problem can be solved more efficiently using a variation of the Boyer Moore Algorithm which is a pattern searching algorithm.