Count substrings of same length differing by a single character from two given strings | Function to count the number of substrings of equal length which differ by a single character ; Stores the count of pairs of substrings ; Traverse the string s ; Traverse the string t ; Different character ; Increment the answer ; Count equal substrings from next index ; Increment the count ; Increment q ; Check the condition ; Increment k ; Add q to count ; Decrement z ; Return the final count ; Driver Code ; Function Call
def countSubstrings ( s , t ) : NEW_LINE INDENT answ = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT for j in range ( len ( t ) ) : NEW_LINE INDENT if t [ j ] != s [ i ] : NEW_LINE INDENT answ += 1 NEW_LINE k = 1 NEW_LINE z = - 1 NEW_LINE q = 1 NEW_LINE while ( j + z >= 0 <= i + z and s [ i + z ] == t [ j + z ] ) : NEW_LINE INDENT z -= 1 NEW_LINE answ += 1 NEW_LINE q += 1 NEW_LINE DEDENT while ( len ( s ) > i + k and j + k < len ( t ) and s [ i + k ] == t [ j + k ] ) : NEW_LINE INDENT k += 1 NEW_LINE answ += q NEW_LINE z = - 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return answ NEW_LINE DEDENT S = " aba " NEW_LINE T = " baba " NEW_LINE print ( countSubstrings ( S , T ) ) NEW_LINE