Remove the forbidden strings | pre [ ] keeps record of the characters of w that need to be changed ; stores the forbidden strings ; Function to check if the particula r substring is present in w at position ; If length of substring from this position is greater than length of w then return ; n and n1 are used to check for substring without considering the case of the letters in w by comparing the difference of ASCII values ; If same == true then it means a substring was found starting at position therefore all characters from position to length of substring found need to be changed therefore they needs to be marked ; Function implementing logic ; To verify if any substring is starting from index i ; Modifying the string w according to th rules ; This condition checks if w [ i ] = upper ( letter ) ; This condition checks if w [ i ] is any lowercase letter apart from letter . If true replace it with letter ; This condition checks if w [ i ] is any uppercase letter apart from letter . If true then replace it with upper ( letter ) . ; number of forbidden strings ; given string ; letter to replace and occur max number of times ; Calling function
pre = [ False ] * 100 NEW_LINE s = [ None ] * 110 NEW_LINE def verify ( position , index ) : NEW_LINE INDENT l = len ( w ) NEW_LINE k = len ( s [ index ] ) NEW_LINE if ( position + k > l ) : NEW_LINE INDENT return NEW_LINE DEDENT same = True NEW_LINE for i in range ( position , position + k ) : NEW_LINE INDENT ch = w [ i ] NEW_LINE ch1 = s [ index ] [ i - position ] NEW_LINE if ( ch >= ' a ' and ch <= ' z ' ) : NEW_LINE INDENT n = ord ( ch ) - ord ( ' a ' ) NEW_LINE DEDENT else : NEW_LINE INDENT n = ord ( ch ) - ord ( ' A ' ) NEW_LINE DEDENT if ( ch1 >= ' a ' and ch1 <= ' z ' ) : NEW_LINE INDENT n1 = ord ( ch1 ) - ord ( ' a ' ) NEW_LINE DEDENT else : NEW_LINE INDENT n1 = ord ( ch1 ) - ord ( ' A ' ) NEW_LINE DEDENT if ( n != n1 ) : NEW_LINE INDENT same = False NEW_LINE DEDENT DEDENT if ( same == True ) : NEW_LINE INDENT for i in range ( position , position + k ) : NEW_LINE INDENT pre [ i ] = True NEW_LINE DEDENT return NEW_LINE DEDENT DEDENT def solve ( ) : NEW_LINE INDENT l = len ( w ) NEW_LINE p = ord ( letter ) - ord ( ' a ' ) NEW_LINE for i in range ( l ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT verify ( i , j ) NEW_LINE DEDENT DEDENT for i in range ( l ) : NEW_LINE INDENT if ( pre [ i ] == True ) : NEW_LINE INDENT if ( w [ i ] == letter ) : NEW_LINE INDENT w [ i ] = ' b ' if ( letter == ' a ' ) else ' a ' NEW_LINE DEDENT elif ( w [ i ] == str ( ord ( ' A ' ) + p ) ) : NEW_LINE INDENT w [ i ] = ' B ' if ( letter == ' a ' ) else ' A ' NEW_LINE DEDENT elif ( w [ i ] >= ' a ' and w [ i ] <= ' z ' ) : NEW_LINE INDENT w [ i ] = letter NEW_LINE DEDENT elif ( w [ i ] >= ' A ' and w [ i ] <= ' Z ' ) : NEW_LINE INDENT w [ i ] = chr ( ord ( ' A ' ) + p ) NEW_LINE DEDENT DEDENT DEDENT print ( ' ' . join ( w ) ) NEW_LINE DEDENT n = 3 NEW_LINE s [ 0 ] = " etr " NEW_LINE s [ 1 ] = " ed " NEW_LINE s [ 2 ] = " ied " NEW_LINE w = " PEtrUnited " NEW_LINE w = list ( w ) NEW_LINE letter = ' d ' NEW_LINE solve ( ) NEW_LINE