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?的更多相关文章

  1. No.003:Longest Substring Without Repeating Characters

    问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ...

  2. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  3. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  4. [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  5. Leetcode经典试题:Longest Substring Without Repeating Characters解析

    题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...

  6. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  7. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  8. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  9. 3. Longest Substring Without Repeating Characters(c++) 15ms

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

随机推荐

  1. Message Queue协议AMQP

    历史: Message Queue的需求由来已久,80年代最早在金融交易中,高盛等公司采用Teknekron公司的产品,当时的Message queuing软件叫做:the information b ...

  2. android依据区域高度切割文本问题

    android字体显示涉及例如以下參数:1. 基准点是baseline:2. Ascent是baseline之上至字符最高处的距离:3. Descent是baseline之下至字符最低处的距离.4.  ...

  3. Html学习笔记3

    1表格的标题和表头: <table> <caption>成绩单</caption> <tr> <th>姓名</th> <t ...

  4. Java基础加强总结(三)——代理(Proxy)

    一.代理的概念 动态代理技术是整个java技术中最重要的一个技术,它是学习java框架的基础,不会动态代理技术,那么在学习Spring这些框架时是学不明白的. 动态代理技术就是用来产生一个对象的代理对 ...

  5. jQuery遍历刚创建的元素

    对于刚创建的元素,使用jQuery的each方法,有时候会不起作用.解决方案大致有2种: 1.刚创建完的时候,就使用each方法 $('#btn').on("click", fun ...

  6. EBS查询默认应用用户,比如是否需要锁定、修改这些用户

    /* Formatted on 2018/3/15 13:05:39 (QP5 v5.256.13226.35538) */ --查询默认应用用户,比如是否需要锁定.修改这些用户 SELECT ROW ...

  7. 【C++】拷贝构造函数和赋值符函数

    在C++中,调用拷贝构造函数有三种情况: 1.一个对象作为函数参数,以值传递的方式传入函数体. 2.一个对象作为函数返回值,以值传递的方式从函数返回. 3.一个对象用于给另外一个对象进行初始化(复制初 ...

  8. Linux下Tomcat的启动、关闭

    在Linux系统下,启动和关闭Tomcat使用命令操作. 进入Tomcat下的bin目录 1 cd /java/tomcat/bin 启动Tomcat命令 1 ./startup.sh 停止Tomca ...

  9. QML与C++交互:在qml中使用QSqlQueryModel显示数据库数据

    QML与C++交互:在qml中使用QSqlQueryModel显示数据库数据 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 參考链接: http:// ...

  10. 关闭Windows Server 2012的IE增强安全配置

    在Windows Server 2012中,IE的安全性被增强,对于没有加入信任站点的网址会弹出提示框: 微软这样做是为了增强IE的安全性,但是在实际的使用过程中并不是很方便.如果是个人电脑安装了Wi ...