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
Explanation:
These are 3 strings which come lexicographically in between S1 and S2 i.e. “c”, “d” & “e”
Input: S1 = “aby”, S2 = “ace”
Output: 5
Explanation:
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 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)
For example:
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:
C++
#include <bits/stdc++.h>
using namespace std;
int LexicoLesserStrings(string s)
{
int count = 0;
int len;
len = s.size();
for ( int i = 0; i < len; i++) {
count += (s[i] - 'a' )
* pow (26, len - i - 1);
}
return count;
}
int countString(string S1, string S2)
{
int countS1, countS2, totalString;
countS1 = LexicoLesserStrings(S1);
countS2 = LexicoLesserStrings(S2);
totalString = countS2 - countS1 - 1;
return (totalString < 0 ? 0 : totalString);
}
int main()
{
string S1, S2;
S1 = "cda" ;
S2 = "cef" ;
cout << countString(S1, S2);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int LexicoLesserStrings(String s)
{
int count = 0 ;
int len;
len = s.length();
for ( int i = 0 ; i < len; i++)
{
count += (s.charAt(i) - 'a' ) *
Math.pow( 26 , len - i - 1 );
}
return count;
}
static int countString(String S1, String S2)
{
int countS1, countS2, totalString;
countS1 = LexicoLesserStrings(S1);
countS2 = LexicoLesserStrings(S2);
totalString = countS2 - countS1 - 1 ;
return (totalString < 0 ? 0 : totalString);
}
public static void main(String args[])
{
String S1, S2;
S1 = "cda" ;
S2 = "cef" ;
System.out.println(countString(S1, S2));
}
}
|
Python3
def LexicoLesserStrings(s):
count = 0
length = len (s)
for i in range (length):
count + = (( ord (s[i]) - ord ( 'a' )) *
pow ( 26 , length - i - 1 ))
return count
def countString(S1, S2):
countS1 = LexicoLesserStrings(S1)
countS2 = LexicoLesserStrings(S2)
totalString = countS2 - countS1 - 1 ;
return ( 0 if totalString < 0 else totalString)
S1 = "cda" ;
S2 = "cef" ;
print (countString(S1, S2))
|
C#
using System;
class GFG{
static int LexicoLesserStrings(String s)
{
int count = 0;
int len;
len = s.Length;
for ( int i = 0; i < len; i++)
{
count += ((s[i] - 'a' ) *
( int )Math.Pow(26, len - i - 1));
}
return count;
}
static int countString(String S1, String S2)
{
int countS1, countS2, totalString;
countS1 = LexicoLesserStrings(S1);
countS2 = LexicoLesserStrings(S2);
totalString = countS2 - countS1 - 1;
return (totalString < 0 ? 0 : totalString);
}
public static void Main()
{
String S1, S2;
S1 = "cda" ;
S2 = "cef" ;
Console.Write(countString(S1, S2));
}
}
|
Javascript
<script>
function LexicoLesserStrings(s) {
var count = 0;
var len;
len = s.length;
for ( var i = 0; i < len; i++) {
count +=
(s[i].charCodeAt(0) - "a" .charCodeAt(0)) *
Math.pow(26, len - i - 1);
}
return count;
}
function countString(S1, S2) {
var countS1, countS2, totalString;
countS1 = LexicoLesserStrings(S1);
countS2 = LexicoLesserStrings(S2);
totalString = countS2 - countS1 - 1;
return totalString < 0 ? 0 : totalString;
}
var S1, S2;
S1 = "cda" ;
S2 = "cef" ;
document.write(countString(S1, S2));
</script>
|
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).