[Algorithm] Longest Substring Without Repeating Characters?
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is"abc", with the length of 3.Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is"b", with the length of 1.Example 3:
Input: "pwwkew"
Output: 3
Explanation: 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.
Solution:
In the naive approaches, we repeatedly check a substring to see if it has duplicate character. But it is unnecessary. If a substring s_{ij}sij from index ii to j - 1j−1 is already checked to have no duplicate characters. We only need to check if s[j]s[j] is already in the substring s_{ij}sij.
To check if a character is already in the substring, we can scan the substring, which leads to an O(n^2)O(n2) algorithm. But we can do better.
By using HashSet as a sliding window, checking if a character in the current can be done in O(1)O(1).
A sliding window is an abstract concept commonly used in array/string problems. A window is a range of elements in the array/string which usually defined by the start and end indices, i.e. [i, j)[i,j) (left-closed, right-open). A sliding window is a window "slides" its two boundaries to the certain direction. For example, if we slide [i, j)[i,j) to the right by 11 element, then it becomes [i+1, j+1)[i+1,j+1) (left-closed, right-open).
Back to our problem. We use HashSet to store the characters in current window [i, j)[i,j) (j = ij=i initially). Then we slide the index jj to the right. If it is not in the HashSet, we slide jj further. Doing so until s[j] is already in the HashSet. At this point, we found the maximum size of substrings without duplicate characters start with index ii. If we do this for all ii, we get our answer.
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
let begin = 0, max = 0;
let hash = new Set(); for (let end = 0; end < s.length; end++) {
if (hash.has(s[end])) {
while (s[begin] !== s[end]) {
// delete chars until the dulpicate one
hash.delete(s[begin++]);
}
// delete dulpicate one
hash.delete(s[begin++]);
} hash.add(s[end])
max = Math.max(max, hash.size) }
return max;
};
[Algorithm] Longest Substring Without Repeating Characters?的更多相关文章
- No.003:Longest Substring Without Repeating Characters
问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 3. Longest Substring Without Repeating Characters(c++) 15ms
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
随机推荐
- LPC43xx SGPIO Slice 示意图
SGPIO inverted clock qualifier Hi, With bits 6:5 of SGPIO_MUX_CFG the QUALIFIER_MODE is selected (0x ...
- vbs学习笔记2——创建桌面快捷方式
脚本 Set WshShell = WScript.CreateObject("WScript.Shell") strDesktop = WshShell.SpecialFolde ...
- [置顶] Android系统移植与调试之------->build.prop文件详细赏析
小知识:什么是build.prop? /system/build.prop 是一个属性文件,在Android系统中.prop文件很重要,记录了系统的设置和改变,类似於/etc中的文件.这个文件是如 ...
- Windows Phone本地数据库(SQLCE):8、DataContext(翻译)
这是“windows phone mango本地数据库(sqlce)”系列短片文章的第八篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知 ...
- iOS: 计算 UIWebView 的内容高度
- (void)webViewDidFinishLoad:(UIWebView *)wb { //方法1 CGFloat documentWidth = [[wb stringByEvaluating ...
- transitionFromView方法的使用
transitionFromView方法的使用 效果 源码 // // ViewController.m // TransitionFromView // // Created by YouXianM ...
- JavaScript中 location.host 与 location.hostname 的区别
JavaScript 中,大多数情况下,我们不会发现 location.host 与 location.hostname 的区别,因为大多数情况下,我们的网页用的是 80 端口. 他们的区别: loc ...
- 灵书妙探第一季/全集Castle迅雷下载
第一季 Castle Season 1 (2009)看点:ABC电视台2009年开播的一部罪案剧,讲述一位罪案小说家Richard Castle帮助纽约警察局凶杀组破案的故事.凶案组女警探Kate B ...
- 将hta包装为exe发布
hta在打开的时候,有时候会被杀毒软件拦截而不给执行,更重要的一点是通常都可以右击查看源代码,里面如果涉及到域名或者其它的一些细节就很容易被其它人了解. 网络上有一些hta转exe的,类似的软件基本上 ...
- 手游开发Android平台周边工具介绍
1.渠道接入 主要是需要接入各平台的登录.充值接口,各家SDK又不统一,Android渠道都是鱼龙混杂,就算小渠道你看不上,但量多了,加起来也还可观,所以大家都拿出吃奶的尽去铺渠道.国内几大主要的An ...