Hamming Distance Discussions | Algorithms | HackerRank

Sort by

recency

|

29 Discussions

|

  • + 0 comments

    after reading the editorial I undertand why this problem is expert the proposed sol uses musch less memory (unimportant, my uses 100k) and is much more fast (may be 32 (or 64) times faster ?) BUT using basic C my sol runs in less than 1 sec on test case 12 (50k string) the only I have unlocked All test run whith time limit so why use a contrived solution?

  • + 0 comments

    why this problem is valutaed so high ? expert 150 points ?

    solved with very basic coding, no problem at all Why 50 points problem are so hard ?

  • + 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]

  • + 0 comments

    Here is Hamming Distance problem solution in Python Java C++ and c programming - https://programs.programmingoneonone.com/2021/07/hackerrank-hamming-distance-problem-solution.html

  • + 0 comments

    How to make this code run within time limit?

    def change(string, l, r, ch):
        l1 = list(string)
        first = "".join(l1[:l-1])
        for i in range(l-1, r):
            l1[i] = ch
        str1 = "".join(l1[l-1:r])
        last = "".join(l1[r:])
        rtnStr = first+str1+last
        return rtnStr
    
    
    def swap(string, l1, r1, l2, r2):
        l = list(string)
        first = "".join(l[:l1-1])
        str1 = "".join(l[l1-1:r1])
        str3 = "".join(l[r1:l2-1])
        str2 = "".join(l[l2-1:r2])
        last = "".join(l[r2:])
        rtnStr = first+str2+str3+str1+last
        # print(rtnStr)
        return rtnStr
    
    
    def reverse(string, l, r):
        l1 = list(string)
        first = "".join(l1[:l-1])
        nstr = "".join(l1[l-1:r])
        rstr = nstr[::-1]
        last = "".join(l1[r:])
    
        rtnStr = first+rstr+last
        # print(rtnStr)
        return rtnStr
    
    
    def hamming(string, l1, l2, r):
        str1 = string[l1-1:l1+r-1]
        str2 = string[l2-1:l2+r-1]
        count = 0
        for i in range(0, r):
            if str1[i] != str2[i]:
                count += 1
        print(count)
    
    
    def solveQuery(givenString, *a):
        if a[0] == 'C':
            givenString = change(givenString, int(a[1]), int(a[2]), a[3])
        elif a[0] == 'S':
            givenString = swap(givenString, int(
                a[1]), int(a[2]), int(a[3]), int(a[4]))
        elif a[0] == 'R':
            givenString = reverse(givenString, int(a[1]), int(a[2]))
        elif a[0] == 'W':
            print(givenString[int(a[1]) - 1:int(a[2])])
        else:
            hamming(givenString, int(a[1]), int(a[2]), int(a[3]))
        return givenString
    
    
    def callFunction(l, n, s):
        for i in range(0, n):
            prams = l[i].split(" ")
            s = solveQuery(s, *prams)
    
    
    if __name__ == "__main__":
        l = int(input())
        s = input()
        n = int(input())
        querys = []
        for i in range(0, n):
            querys.append(input())
        callFunction(querys, n, s)