python-求字符串不重复的最长子串及其长度(饿了么)

 

#!/usr/bin/python

#encapsulation
'''
def judge(st):
    n = len(st)
    if n==0 or st is None:
        return None
    print('The argument passed in is %s' % st)
    newstr=''
    for s in st:
        if s.isalpha():
            newstr+=s
    N=len(newstr)
    if N==0:
        return None
    elif N==1:
        print('The longest substr of %s is %s,length:%s' % (newstr,newstr,N))
        return newstr,N 
    print('The parameter after processing is %s' % newstr)
'''
    
def lsnoDup(st):
    #judge is valid string or not, if not,convert it to be
    n = len(st)
    if n==0 or st is None:
        return None
    print('The argument passed in is %s' % st)
    newstr=''
    for s in st:
        if s.isalpha():
            newstr+=s
    N=len(newstr)
    if N==0:
        return None
    elif N==1:
        print('The longest substr of %s is %s,length:%s' % (newstr,newstr,N))
        return newstr,N 
    print('The parameter after processing is %s' % newstr)
    
    
    dic = {}
    lst = []
    #startIndex = 0
    maxLen = 0
    for i in range(0,N):
        for j in range(i,N):	    
	    if newstr[j] not in lst and (j-i)<=len(lst):
	        lst.append(newstr[j])
	    else:
	        #how to record startIndex?
	        #startIndex = i
		if maxLen<len(lst):
		    maxLen = len(lst)
		    dic={}
	            dic[''.join(lst)] = len(lst)
		    
	        lst=[]
		continue
    #dic2 = sorted(dic.items(), key=lambda x: x[1], reverse=True)
    k = dic.keys()[0]
    k=''.join(k)
    v = dic.values()[0] 
    print('The longest substr of %s is %s,length:%s' % (newstr,k,v))
    return dic
    
str1 = '  abca  dbcbb   '
#'bcad'  assert
str2 = '   $sd1#111$sv    da123!!!221&eSSDSDG^svda121^svda124^   '
str3 = '123'
res1 = lsnoDup(str1.strip())
res2 = lsnoDup(str2.strip())
res3 = lsnoDup(str3.strip())
print(res1)
print(res2)
print(res3)
    

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值