[Leetcode] - Longest Substring Without Repeating Characters

11 篇文章 0 订阅

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.


这两天一直在努力研究各种substring相关的问题,于是在Leetcode上面找到了这道题,复习一下当时的想法。具体思路是这样的,用一个HashMap来记录每个出现的字符和它们在字符串中所出现的位置,也就是index。所以说,这个HashMap应该被初始化为HashMap<Character, Integer>。用start和end两个变量来保存当前NRCS(Non-Repeating Character Substring)的起始和终止位置,说白了就是一个窗口,start初始为0,end用作循环里面自增的变量,end的大小等于string长度的时候结束循环。最后用len这个变量记录最长的NRCS的长度,将其初始化为0,因为NRCS的长度至少为1。

有了这些东西之后,我们就可以开始遍历这个string求答案了。对于每个字符,如果HashMap中没有,就将它以及它的index放进去。如果HashMap中已经有了这个Key的话,我们需要做如下几件事情:

1. 首先,把这个字符以前出现的index从HashMap里面拿出来,存在变量index里面。

2. 然后,比较end-start和len的大小,如果需要的话,更新len的值。(注意:end-start得到的值就是当前NRCS的长度)

3. 接着,检查字符串在start和index间的所有字符,对于每个字符,把HashMap中对应的key-value pair删除。

4. 最后,将start设置为这个index加1的位置。

还有一点需要注意的是,如果这个字符串没有重复的字符,那么上面的方法将不会更新len的值。所以,在循环结束之后,还需要比较一次end-start和len的大小,需要时update一下len的值。

综上所述,time complexity是O(n)。由于用了HashMap,所以space complexity是O(d),这里面d是字符串中不重复字符的个数。


代码如下:

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        if(s==null || s.length()==0) return 0;
        if(s.length()==1) return 1;
        
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        int start=0, end=1, len=Integer.MIN_VALUE;
        map.put(s.charAt(start), 0);
        while(end < s.length()) {
            if(map.containsKey(s.charAt(end))) {    // find duplicate characters
                len = Math.max(len, end-start);     // update the maximum length if necessary
                int index = map.get(s.charAt(end));
                for(int k=start; k<=index; k++) {
                    map.remove(s.charAt(k));
                }
                start = index+1;
            }
            map.put(s.charAt(end), end);
            end++;
        }
        len = Math.max(len, end-start);     // in case there is no duplicate in this string
        return len;
    }
}

捎带手的复习一下substring,subsequence,prefix,suffix等的定义,下面是wikipedia里面的一段话:

substring of a string S is another string S' that occurs "in" S. For example, "the best of" is a substring of "It was the best of times". This is not to be confused with subsequence, which is a generalization of substring. For example, "Itwastimes" is a subsequence of "It was the best of times", but not a substring.

Prefix and suffix are refinements of substring. A prefix of a string S is a substring of S that occurs at the beginning of S. A suffix of a string Sis a substring that occurs at the end of S.

Not including the empty substring, the number of substrings of a string of length n where symbols only occur once, is the number of ways to choose two distinct places between symbols to start/end the substring. Including the very beginning and very end of the string, there are n+1 such places. So there are \tbinom{n+1}{2} = \tfrac{n(n+1)}{2} non-empty substrings.


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值