Given a String S, consider a new string formed by repeating the S exactly K times. We need find the number of subsequences as “ab” in the newly formed string.
Input : S = "abcb" K = 2 Output : 6 Here, Given string is repeated 2 times and we get a new string "abcbabcb" Below are 6 occurrences of "ab" abcbabcb abcbabcb abcbabcb abcbabcb abcbabcb abcbabcb Input : S = "aacbd" K = 1 Output : 2
Naive Approach: Finding no.of subsequences of “ab” is in fact finding a pair s[i], s[j] (i < j) where s[i] = ‘a’, s[j] = ‘b’.
We can do this by using two nested for loops and count the no. of pairs.
We can improve this approach in a single traversal of the string. Let us consider an index j, s[j] =’b’, if we find no.of index i‘s such that s[i] = ‘a’ and i < j, then it is same as no.of subsequences of "ab" till j. This can be done by maintaining count of a’s by traversing the array and add the count to our answer at position where s[i] =’b .
Let T be the newly formed string
T = s1 + s2 + s3 + ….. + sk;
where si is the ith occurrence of the string s.
Here, occurrence of “ab” in T are as follows:
1)”ab” lies completely in the some of occurrence of string S, so we can simply find occurrences of “ab” in Si.Let it be C. So, total no.of occurrences of “ab” in T will be C*K.
2) Otherwise, ”a” lies strictly inside some string Si and “b” lies inside some other string Sj, (i < j). In this way finding no.of occurrences of "ab" will be choosing two occurrences of string S from K occurrences(KC2) and multiplying it with no. of occurrences of “a” in Si and no.of occurrences of “b” in Sj.
As, Si = Sj = S.
Time Complexity: O(|S|), for counting no.of “a”s and no.of “b”s.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- String which when repeated exactly K times gives a permutation of S
- Longest sub string of 0's in a binary string which is repeated K times
- Check if any permutation of string is a K times repeated string
- Number of subsequences of maximum length K containing no repeated elements
- Minimum number of times A has to be repeated such that B is a substring of it
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Number of subsequences in a string divisible by n
- Find number of times a string occurs as a subsequence in given string
- Number of subsequences in a given binary string divisible by 2
- Number of ways to partition a string into two balanced subsequences
- Minimum number of subsequences required to convert one string to another
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Minimum number of palindromic subsequences to be removed to empty a binary string
- C++ program to concatenate a string given number of times
- Repeat substrings of the given String required number of times
- Number of times the given string occurs in the array in the range [l, r]
- Check if a string can be repeated to make another string
- Count subsequences in first string which are anagrams of the second string
- String obtained by reversing and complementing a Binary string K times
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.