Given a string S and an integer K, the task is to find the total number of strings that can be formed by inserting exactly K characters at any position of the string S. Since the answer can be large, print it modulo 109+7.
Input: S = “a” K = 1
Since any of the 26 characters can be inserted at before ‘a’ or after ‘a’, a total of 52 possible strings can be formed.
But the string “aa” gets formed twice. Hence count of distinct strings possible is 51.
Input: S = “abc” K = 2
The idea is to find the number of strings that contains the str as a subsequence. Follow the steps below to solve the problem:
- The total number of strings that can be formed by N characters is 26N.
- Calculate 26N using Binary Exponentiation.
- In this problem, only the strings that contain the str as a subsequence needs to be considered.
- Hence, the final count of strings is given by
(total number of strings) – (number of strings that don’t contain the input string as a sub-sequence)
- While calculating such strings that don’t contain the str as a subsequence, observe that the length of the prefix of S is a subsequence of the resulting string can be between 0 to |S|-1.
- For every prefix length from 0 to |S|-1, find the total number of strings that can be formed with such a prefix as a sub-sequence. Then subtract that value from 26N.
- Hence, the final answer is:
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Find distinct characters in distinct substrings of a string
- String with k distinct characters and no same characters adjacent
- Count the number of strings in an array whose distinct characters are less than equal to M
- Sort an array of strings based on count of distinct characters
- Count of distinct possible strings after performing given operations
- Find the number of strings formed using distinct characters of a given string
- Count of Palindromic Strings possible by swapping of a pair of Characters
- Largest palindromic string possible from given strings by rearranging the characters
- Count of ordered triplets (R, G, B) in a given original string
- Replace minimal number of characters to make all characters pair wise distinct
- Decode a median string to the original string
- Restore original String from given Encrypted String by the given operations
- Check whether count of distinct characters in a string is Prime or not
- Count ways to partition a string such that both parts have equal distinct characters
- String Range Queries to count number of distinct characters with updates
- Count of distinct permutations of every possible length of given string
- Distinct strings such that they contains given strings as sub-sequences
- Find the minimum number of rectangles left after inserting one into another
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.