码迷,mamicode.com
首页 > 其他好文 > 详细

[Algo] 253. Longest Substring Without Repeating Characters

时间:2020-02-20 22:14:59      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:==   nta   contain   array   add   move   +=   eating   string   

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

标签:==   nta   contain   array   add   move   +=   eating   string   

原文地址:https://www.cnblogs.com/xuanlu/p/12337557.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!