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.

**Examples:**

Input:S1 = “b”, S2 = “f”

Output:3

Explaination:

These are 3 strings which come lexicographically in between S1 and S2 i.e. “c”, “d” & “e”

Input:S1 = “aby”, S2 = “ace”

Output:5

Explaination:

These are 5 strings which come lexicographically in between S1 and S2 i.e. “abz”, “aca”, “acb”, “acc” & “acd”.

**Approach:**

- 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**c**where c_{0}c_{1}c_{2}...c_{L-1}_{i}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 c_{0}* 26^{L-1}) + (number of letters less than c_{1}* 26^{L-2}) + (number of letters less than c_{2}* 26^{L-3}) + ... + (number of letters less than c_{L-2}* 26) + (number of letters less than c_{L-1})**For example:**Let S1 = "cbd" Number of strings less than S1 N(S1) = (number of letters less than 'c' * 26

^{2}) + (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:

## C++

`// C++ program to find the count of ` `// same length Strings that exists lexicographically ` `// in between two given Strings ` ` ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the count of strings less ` `// than given string lexicographically ` `int` `LexicoLesserStrings(string s) ` `{ ` ` ` `int` `count = 0; ` ` ` `int` `len; ` ` ` ` ` `// Find length of string s ` ` ` `len = s.size(); ` ` ` ` ` `// Looping over the string characters and ` ` ` `// finding strings less than that character ` ` ` `for` `(` `int` `i = 0; i < len; i++) { ` ` ` `count += (s[i] - ` `'a'` `) ` ` ` `* ` `pow` `(26, len - i - 1); ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Function to find the count of ` `// same length Strings that exists ` `// lexicographically in between two given Strings ` `int` `countString(string S1, string S2) ` `{ ` ` ` `int` `countS1, countS2, totalString; ` ` ` ` ` `// Count string less than S1 ` ` ` `countS1 = LexicoLesserStrings(S1); ` ` ` ` ` `// Count string less than S2 ` ` ` `countS2 = LexicoLesserStrings(S2); ` ` ` ` ` `// Total strings between S1 and S2 would ` ` ` `// be difference between the counts - 1 ` ` ` `totalString = countS2 - countS1 - 1; ` ` ` ` ` `// If S1 is lexicographically greater ` ` ` `// than S2 then return 0, otherwise return ` ` ` `// the value of totalString ` ` ` `return` `(totalString < 0 ? 0 : totalString); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string S1, S2; ` ` ` `S1 = ` `"cda"` `; ` ` ` `S2 = ` `"cef"` `; ` ` ` ` ` `cout << countString(S1, S2); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the count of same length ` `// Strings that exists lexicographically ` `// in between two given Strings ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find the count of strings less ` `// than given string lexicographically ` `static` `int` `LexicoLesserStrings(String s) ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` `int` `len; ` ` ` ` ` `// Find length of string s ` ` ` `len = s.length(); ` ` ` ` ` `// Looping over the string characters and ` ` ` `// finding strings less than that character ` ` ` `for` `(` `int` `i = ` `0` `; i < len; i++) ` ` ` `{ ` ` ` `count += (s.charAt(i) - ` `'a'` `) * ` ` ` `Math.pow(` `26` `, len - i - ` `1` `); ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Function to find the count of ` `// same length Strings that exists ` `// lexicographically in between two ` `// given Strings ` `static` `int` `countString(String S1, String S2) ` `{ ` ` ` `int` `countS1, countS2, totalString; ` ` ` ` ` `// Count string less than S1 ` ` ` `countS1 = LexicoLesserStrings(S1); ` ` ` ` ` `// Count string less than S2 ` ` ` `countS2 = LexicoLesserStrings(S2); ` ` ` ` ` `// Total strings between S1 and S2 would ` ` ` `// be difference between the counts - 1 ` ` ` `totalString = countS2 - countS1 - ` `1` `; ` ` ` ` ` `// If S1 is lexicographically greater ` ` ` `// than S2 then return 0, otherwise return ` ` ` `// the value of totalString ` ` ` `return` `(totalString < ` `0` `? ` `0` `: totalString); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `String S1, S2; ` ` ` `S1 = ` `"cda"` `; ` ` ` `S2 = ` `"cef"` `; ` ` ` ` ` `System.out.println(countString(S1, S2)); ` `} ` `} ` ` ` `// This code is contributed by apurva raj ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the count of same ` `# length Strings that exists lexicographically ` `# in between two given Strings ` ` ` `# Function to find the count of strings less ` `# than given string lexicographically ` `def` `LexicoLesserStrings(s): ` ` ` ` ` `count ` `=` `0` ` ` ` ` `# Find length of string s ` ` ` `length ` `=` `len` `(s) ` ` ` ` ` `# Looping over the string characters and ` ` ` `# finding strings less than that character ` ` ` `for` `i ` `in` `range` `(length): ` ` ` `count ` `+` `=` `((` `ord` `(s[i]) ` `-` `ord` `(` `'a'` `)) ` `*` ` ` `pow` `(` `26` `, length ` `-` `i ` `-` `1` `)) ` ` ` ` ` `return` `count ` ` ` `# Function to find the count of ` `# same length Strings that exists ` `# lexicographically in between two ` `# given Strings ` `def` `countString(S1, S2): ` ` ` ` ` `# Count string less than S1 ` ` ` `countS1 ` `=` `LexicoLesserStrings(S1) ` ` ` ` ` `# Count string less than S2 ` ` ` `countS2 ` `=` `LexicoLesserStrings(S2) ` ` ` ` ` `# Total strings between S1 and S2 would ` ` ` `# be difference between the counts - 1 ` ` ` `totalString ` `=` `countS2 ` `-` `countS1 ` `-` `1` `; ` ` ` ` ` `# If S1 is lexicographically greater ` ` ` `# than S2 then return 0, otherwise return ` ` ` `# the value of totalString ` ` ` `return` `(` `0` `if` `totalString < ` `0` `else` `totalString) ` ` ` `# Driver code ` `S1 ` `=` `"cda"` `; ` `S2 ` `=` `"cef"` `; ` ` ` `print` `(countString(S1, S2)) ` ` ` `# This code is contributed by apurva raj ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the count of same length ` `// Strings that exists lexicographically ` `// in between two given Strings ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the count of strings less ` `// than given string lexicographically ` `static` `int` `LexicoLesserStrings(String s) ` `{ ` ` ` `int` `count = 0; ` ` ` `int` `len; ` ` ` ` ` `// Find length of string s ` ` ` `len = s.Length; ` ` ` ` ` `// Looping over the string characters and ` ` ` `// finding strings less than that character ` ` ` `for` `(` `int` `i = 0; i < len; i++) ` ` ` `{ ` ` ` `count += ((s[i] - ` `'a'` `) * ` ` ` `(` `int` `)Math.Pow(26, len - i - 1)); ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Function to find the count of ` `// same length Strings that exists ` `// lexicographically in between two ` `// given Strings ` `static` `int` `countString(String S1, String S2) ` `{ ` ` ` `int` `countS1, countS2, totalString; ` ` ` ` ` `// Count string less than S1 ` ` ` `countS1 = LexicoLesserStrings(S1); ` ` ` ` ` `// Count string less than S2 ` ` ` `countS2 = LexicoLesserStrings(S2); ` ` ` ` ` `// Total strings between S1 and S2 would ` ` ` `// be difference between the counts - 1 ` ` ` `totalString = countS2 - countS1 - 1; ` ` ` ` ` `// If S1 is lexicographically greater ` ` ` `// than S2 then return 0, otherwise return ` ` ` `// the value of totalString ` ` ` `return` `(totalString < 0 ? 0 : totalString); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `String S1, S2; ` ` ` `S1 = ` `"cda"` `; ` ` ` `S2 = ` `"cef"` `; ` ` ` ` ` `Console.Write(countString(S1, S2)); ` `} ` `} ` ` ` `// This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

**Output:**

30

**Performance Analysis:**

**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.

**Auxiliary Space Complexity**: As in the above approach there is no extra space used, therefore the Auxiliary Space complexity will be **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.

## Recommended Posts:

- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Count substrings of same length differing by a single character from two given strings
- Count lexicographically increasing K-length strings possible from first N alphabets
- Count N-length strings consisting only of vowels sorted lexicographically
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Check whether two strings contain same characters in same order
- Count binary strings of length same as given string after removal of substrings "01" and "00" that consists of at least one '1'
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Compare two strings lexicographically in Java
- Maximum count of sub-strings of length K consisting of same characters
- Lexicographically next greater string using same character set
- Meta Strings (Check if two strings can become same after a swap in one string)
- Lexicographically smallest string which differs from given strings at exactly K indices
- Loss when two items are sold at same price and same percentage profit/loss
- Count arrays of length K whose product of elements is same as that of given array
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Lexicographically smallest string of length N and sum K
- Lexicographically smallest K-length substring containing maximum number of vowels
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Minimize replacements or swapping of same indexed characters required to make two given strings palindromic

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.