Longest Substring Without Repeating Characters
Description
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequence and not a substring.
Tags: Hash Table, Two Pointers, String
思路
题意是计算不带重复字符的最长子字符串的长度,开辟一个 hash 数组来存储该字符上次出现的位置,比如 hash[a] = 3
就是代表 a
字符前一次出现的索引在 3,遍历该字符串,获取到上次出现的最大索引(只能向前,不能退后),与当前的索引做差获取的就是本次所需长度,从中迭代出最大值就是最终答案。
class Solution {
public int lengthOfLongestSubstring(String s) {
int len;
if (s == null || (len = s.length()) == 0) return 0;
int preP = 0, max = 0;
int[] hash = new int[128];
for (int i = 0; i < len; ++i) {
char c = s.charAt(i);
if (hash[c] > preP) {
preP = hash[c];
}
int l = i - preP + 1;
hash[c] = i + 1;
if (l > max) max = l;
}
return max;
}
}
class Solution {
fun lengthOfLongestSubstring(s: String): Int {
val n = s.length
var ans = 0
val index = IntArray(128) // current index of character
// try to extend the range [i, j]
var j = 0
var i = 0
while (j < n) {
i = Math.max(index[s[j].toInt()], i)
ans = Math.max(ans, j - i + 1)
index[s[j].toInt()] = j + 1
j++
}
return ans
}
}
结语
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:LeetCode-Solution