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
def LexicoLesserStrings ( s ) : NEW_LINE INDENT count = 0 NEW_LINE length = len ( s ) NEW_LINE for i in range ( length ) : NEW_LINE INDENT count += ( ( ord ( s [ i ] ) - ord ( ' a ' ) ) * pow ( 26 , length - i - 1 ) ) NEW_LINE DEDENT return count NEW_LINE DEDENT def countString ( S1 , S2 ) : NEW_LINE INDENT countS1 = LexicoLesserStrings ( S1 ) NEW_LINE countS2 = LexicoLesserStrings ( S2 ) NEW_LINE totalString = countS2 - countS1 - 1 ; NEW_LINE return ( 0 if totalString < 0 else totalString ) NEW_LINE DEDENT S1 = " cda " ; NEW_LINE S2 = " cef " ; NEW_LINE print ( countString ( S1 , S2 ) ) NEW_LINE