首页 > 试题广场 >

查找无重复最长子串

[编程题]查找无重复最长子串
  • 热度指数:5085 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给定一个字符串,请找出其中长度最长且不含有重复字符的子串,计算该子串长度。

数据范围:输入的字符串长度满足 ,字符串中仅包含小写的英文字母

输入描述:
输入类型为字符串,例如”abcde“


输出描述:
输出类型为整型, 例如 5
示例1

输入

pwwkew

输出

3

说明

无重复字符的最长子串是"kew",其长度为 3 
strs = input()
ans,res,start ,temp= 0,0,0,[]
for i in strs:
    if i not in temp:
        temp.append(i)
        res = len(temp)
    else:
        start = temp.index(i)
        temp = temp[start:]
    ans = max(ans, res)    
print(ans)     
发表于 2021-05-18 21:31:12 回复(0)
import sys

try:
    while True:
        t = input()
        l = len(t)
        c = 0
        for i in range(l):
            for j in range(l-i):
                lm = len(t[i:l-j])
                A = list(set(t[i:l-j]))
                ln = len(A)
                if lm == ln and lm >= c:
                    c = lm
                    break
        print(c)
except:
    pass

发表于 2020-03-15 20:46:05 回复(0)