Find longest substring without repeating characters

前端 未结 30 2577
轻奢々
轻奢々 2020-12-12 18:07

Given a string S of length N find longest substring without repeating characters.

Example:

Input:

30条回答
  •  忘掉有多难
    2020-12-12 19:05

    I am posting O(n^2) in python . I just want to know whether the technique mentioned by Karoly Horvath has any steps that are similar to existing search/sort algorithms ?

    My code :

    def main():
        test='stackoverflow'
        tempstr=''
        maxlen,index=0,0
        indexsubstring=''
        print 'Original string is =%s\n\n' %test
    
        while(index!=len(test)):
            for char in test[index:]:
                if char not in tempstr:
                    tempstr+=char
                    if len(tempstr)> len(indexsubstring):
                       indexsubstring=tempstr
                elif (len(tempstr)>=maxlen):
                       maxlen=len(tempstr)
                       indexsubstring=tempstr
                       break
            tempstr=''
            print 'max substring length till iteration with starting index =%s is %s'%(test[index],indexsubstring)
            index+=1
    
    if __name__=='__main__':
        main()
    

提交回复
热议问题