Count of same length Strings that exists lexicographically in between two given Strings | Java 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 ; Find length of string s ; Looping over the string characters and finding strings less than that character ; Function to find the count of same length Strings that exists lexicographically in between two given Strings ; Count string less than S1 ; Count string less than S2 ; Total strings between S1 and S2 would be difference between the counts - 1 ; If S1 is lexicographically greater than S2 then return 0 , otherwise return the value of totalString ; Driver code
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 ) ) ; } }