3 longest substring without repeating characters

1. sliding window:  longest = end - start + 1;

cases need to cover: 

"abcabcabcdd" contains repeated number

"abc" no repeated character: essentially return the length of the string.

for every end, we can get the longest substring ending with s.charAt(end), so using the hash array to record how many times the character appeared already. When (hash[endChar]) > 1, which means the current end is a repeated character, the start is not valid for this substring, we need to move start until hash[endChar] <= 1 again, then end - start + 1 is the length valid for current end. 

scan along, when end reached the boundary, we got the longest length.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容