Hamming Distance Discussions | Algorithms | HackerRank
  • + 1 comment

    My python code passed 20 test case but other 20 test cases showing time exceeded problem.Please any one let me know which lines of code i can optimize.

    N=int(input())

    while True:

    S = input() if len(S) != N: print("Invalid input") exit() M=int(input()) result=[] for i in range(M): cmd=input() c=cmd.split(' ')

    if c[0]=="R":
        Sub=S[int(c[1])-1:int(c[2])]
        S=S[:int(c[1])-1]+Sub[::-1]+S[int(c[2]):]
        #print(S)
    elif c[0]=='W':
        result.append(S[int(c[1])-1:int(c[2])])
        #print(Sub)
    elif c[0]=='C':
    
        len_str=int(c[2])-int(c[1])+1
        S=S[:int(c[1])-1]+(c[3]*len_str)+S[int(c[2]):]
    
    elif c[0]=="H":
        str1=list(S[int(c[1])-1:int(c[1])-1+int(c[3])])
        str2=list(S[int(c[2])-1:int(c[2])-1+int(c[3])])
        equal = list(map(lambda x, y: x == y, str1, str2))
        countt=equal.count(False)     
        result.append(countt)
        #print(count)
    elif c[0]=="S":
        S=S[:int(c[1])-1] + (S[int(c[3])-1:int(c[4])])+ S[int(c[2]):int(c[3])-1]+(S[int(c[1])-1:int(c[2])])+S[int(c[4]):]
        #print(S)
    

    r=[print(i) for i in result]