[Algo] 253. Longest Substring Without Repeating Characters

Posted xuanlu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Algo] 253. Longest Substring Without Repeating Characters相关的知识,希望对你有一定的参考价值。

Given a string, find the longest substring without any repeating characters and return the length of it. The input string is guaranteed to be not null.

For example, the longest substring without repeating letters for "bcdfbd" is "bcdf", we should return 4 in this case.

 

public class Solution {
  public int longest(String input) {
    // Write your solution here
    if (input.length() == 0) {
      return 0;
    }
    char[] charArr = input.toCharArray();
    Set<Character> set = new HashSet<>();
    int res = 0;
    int start = 0, i = 0;
    while (i < charArr.length) {
      char cur = charArr[i];
      if (!set.contains(cur)) {
        set.add(cur);
        res = Math.max(res, i - start + 1);
        i += 1;
      } else {
        set.remove(charArr[start]);
        start += 1;
      }
    }
    return res;
  }
}

 

以上是关于[Algo] 253. Longest Substring Without Repeating Characters的主要内容,如果未能解决你的问题,请参考以下文章

后缀自动机(SAM):SPOJ Longest Common Substring II

395 Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子串

subst是啥命令,怎么使用

SUBST的具体用法是啥?

Haskell 中的打印机用于 Data.Comp.Variables 中的 Subst

如何结合 AC_SUBST 和 AC_DEFINE?