Find an anagram of given String having different characters at corresponding indices | Function to find anagram of string such that characters at the same indices are different ; Copying our original string for comparison ; Declaring the two pointers ; Checking the given condition ; When string length is odd ; The mid element ; If the characters are the same , then perform the swap operation as illustrated ; Check if the corresponding indices has the same character or not ; If string follows required condition ; Driver Code
def findAnagram ( s ) : NEW_LINE INDENT check = s NEW_LINE st = list ( s ) NEW_LINE i = 0 NEW_LINE j = len ( st ) - 1 NEW_LINE while ( i < len ( st ) and j >= 0 ) : NEW_LINE INDENT if ( st [ i ] != st [ j ] and check [ i ] != st [ j ] and check [ j ] != st [ i ] ) : NEW_LINE INDENT st [ i ] , st [ j ] = st [ j ] , st [ i ] NEW_LINE i += 1 NEW_LINE j = len ( st ) - 1 NEW_LINE DEDENT else : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT DEDENT if ( len ( st ) % 2 != 0 ) : NEW_LINE INDENT mid = len ( st ) / 2 NEW_LINE if ( check [ mid ] == st [ mid ] ) : NEW_LINE INDENT for i in range ( len ( st ) ) : NEW_LINE INDENT if ( check [ i ] != st [ mid ] and st [ i ] != st [ mid ] ) : NEW_LINE INDENT st [ i ] , st [ mid ] = st [ mid ] , st [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT ok = True NEW_LINE for i in range ( len ( st ) ) : NEW_LINE INDENT if ( check [ i ] == st [ i ] ) : NEW_LINE INDENT ok = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( ok ) : NEW_LINE INDENT print ( " " . join ( st ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " geek " NEW_LINE findAnagram ( S ) NEW_LINE DEDENT