Longest K-interspace substring
Anonymous User
253

I'm trying to append the last character of word to word but am having trouble implementing it. I added the word += word[-1] to do it but its not working. Any thoughts?

def gap(a,b):
    return abs(ord(a)-ord(b))

def longestKInterspaceSubstring(word, k):
    # Write your code here
    temp,max="",""
    for i in range(len(word)-1):
        temp+=word[i]
        if gap(word[i],word[i+1])>k:
            max=max if len(max)>len(temp) else temp
            temp=""
        word += word[-1]
    return max```
Comments (2)