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.

Solution:

Use two-pointer and HashTable to solve.

Keep p1 at the beginning of the substring and move p2, add characters to hash table and keep the index as value,

if map contains s[p2] then need to  move p1 to the right of the duplicated char index, then update the index of map[s[p2]] to p2;

also p1 and p2 should only move forward;

ex) "abba" when p1=0, p2 = 2, we met the second 'b' then p1 should move to 2, and p2 stay at 2.

then when p1=2, p2=3 we met the second a, p1 should never move back to index 1 to start over;

Before come up with this solution, my solution was to clear the map and move p1 to the right of the first duplicated char, and same to p2 then go through again and add to map,

This method worst case runtime is O(n2), so exceeded the time Limited

 public class Solution {
public int LengthOfLongestSubstring(string s) {
if(string.IsNullOrEmpty(s))
{
return ;
}
int l = s.Length;
int max = ;
int p1=;
int p2=;
Dictionary<char,int> map = new Dictionary<char,int>();
while(p2<l)
{
if(!map.ContainsKey(s[p2]))
{
map.Add(s[p2], p2);
}
else
{
if(p1<=map[s[p2]])
{
p1=map[s[p2]]+; }
map[s[p2]]= p2;
}
max= Math.Max(max, p2-p1+);
p2++; }
return max;
}
}

LeetCode #3. Longest Substring Without Repeating Characters C#的更多相关文章

  1. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  2. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

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

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

  4. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  5. LeetCode之Longest Substring Without Repeating Characters

    [题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...

  6. Leetcode 3. Longest Substring Without Repeating Characters (Medium)

    Description Given a string, find the length of the longest substring without repeating characters. E ...

  7. [Leetcode Week1]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...

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

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  9. LeetCode[3] Longest Substring Without Repeating Characters

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

  10. 【leetcode】Longest Substring Without Repeating Characters

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

随机推荐

  1. OpenCascade

    Hello World of OpenCascade   Hello World of OpenCascade eryar@163.com 摘要Abstract:以一个经典的Hello World程序 ...

  2. asp.net mvc4设置area里面使用相同的 Controller 名称并设置area里的页面为起始页

    asp.net mvc4设置area里面使用相同的 Controller 名称并设置area里的页面为起始页 1.使用重名controller 在asp.net mvc2以后的版本里面,有了area( ...

  3. Mysql re-set password, mysql set encode utf8 mysql重置密码,mysql设置存储编码格式

    There is a link about how to re-set password. http://database.51cto.com/art/201010/229528.htm words ...

  4. VPN pptp on linuxmint13/ubuntu12.04/debian VPS

    VPN is a great thing when you are forced to be stuck somewhere. on ubuntu, you need to check out whe ...

  5. 分享一套 CodeSmit 代码生成模板。

    分享一套 CodeSmit 代码生成模板. 住博客园 5 年了,以前也发过一些博文,但都在 一天后 / 几周后 / 几年后 将它删了:因为感觉代码写得不好:不清晰或侵入太大,哪怕只有一句侵入. 可是最 ...

  6. spring添加通知配置

    在项目里添加的spring配置文件 <bean id="beforeMethod" class="com.wxw.core.common.AdviceBefore& ...

  7. Linux系统 nfs 共享及 挂载mount 配置

    要把 A服务器(192.168.1.1)的 /home 目录挂载到 B服务器(192.168.1.2)的/mnt目录 我们先在 A 服务器上编辑/etc/exports 文件, 添加下面这行代码 /h ...

  8. 30+WordPress古典风格的主题-古典却不失时尚

    通过WordPress古典风格主题,你可以快速的让你的网站呈现古典情调,并在竞争中脱颖而出.古典风格的网页设计是非常受欢迎的,现在,你可能想知道为什么.很多人被古典风格吸引,他们将一些陈旧的,过时的, ...

  9. redis beforesleep

    本来想把redis main函数附带都读完再写笔记,但实在太大了,所以现在决定一部分一部分地记录. eventloop中在每次进入循环时都会调用beforeSleep回调(因为processevent ...

  10. 驱动09.nand flash

    1 nand flash的操作 目的:读地址A的数据,把数据B写到地址A. 问1. 原理图上NAND FLASH和S3C2440之间只有数据线,怎么传输地址?答1.在DATA0-DATA7上既传输数据 ...