Given two string S1 and S2 of length L, the task is to count the number of strings of length L, that exists in between S1 and S2, which are lexicographically greater than S1 but smaller than S2.
Input: S1 = “b”, S2 = “f”
These are 3 strings which come lexicographically in between S1 and S2 i.e. “c”, “d” & “e”
Input: S1 = “aby”, S2 = “ace”
These are 5 strings which come lexicographically in between S1 and S2 i.e. “abz”, “aca”, “acb”, “acc” & “acd”.
- First, find out the number of strings lexicographically smaller than the first string S1, as:
Let the String S1 of length L be represented as c0c1c2...cL-1 where ci is the character in S1 at index i Therefore, To get the number of strings less than S1, we will calculate it as N(S1) = (number of letters less than c0 * 26L-1) + (number of letters less than c1 * 26L-2) + (number of letters less than c2 * 26L-3) + ... + (number of letters less than cL-2 * 26) + (number of letters less than cL-1)
Let S1 = "cbd" Number of strings less than S1 N(S1) = (number of letters less than 'c' * 262) + (number of letters less than 'b' * 26) + (number of letters less than 'd') N(S1) = (2 * 26 * 26) + (1 * 26) + (3) = 1352 + 26 + 3 = 1381.
- Similarly, find out the number of string lexicographically smaller than S2.
- Then just find out the difference between the above two values to get the number of string lexicographically greater than S1 but smaller than S2.
Below is the implementation of the above approach:
Time Complexity: In the above approach, we are looping over the two strings of length N, therefore it will take O(N) time where N is the length of each string.
Auxillary Space Complexity: As in the above approach there is no extra space used, therefore the Auxillary Space complexity will be O(1).
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.
- Count of sub-strings of length n possible from the given string
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count of binary strings of given length consisting of at least one 1
- Count of non-palindromic strings of length M using given N characters
- Count the number of Special Strings of a given length N
- Count ways to increase LCS length of two strings by one
- Count number of binary strings of length N having only 0's and 1's
- Maximum count of sub-strings of length K consisting of same characters
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Count of strings that become equal to one of the two strings after one removal
- Compare two strings lexicographically in Java
- Lexicographically smallest string which differs from given strings at exactly K indices
- Number of ways to divide string in sub-strings such to make them in lexicographically increasing sequence
- Sub-strings of length K containing same character
- All possible strings of any length that can be formed from a given string
- Check if all the palindromic sub-strings are of odd length
- Number of strings of length N with no palindromic sub string
- Number of binary strings such that there is no substring of length ≥ 3
- Sum of all LCP of maximum length by selecting any two Strings at a time
- Find the number of binary strings of length N with at least 3 consecutive 1s
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.