Skip to content

Commit 8c5e128

Browse files
committed
longest substring
1 parent 62e4e67 commit 8c5e128

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

LeetCode.sln

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 2013
4-
VisualStudioVersion = 12.0.0.0
5-
MinimumVisualStudioVersion = 10.0.0.1
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29613.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LeetCode", "LeetCode\LeetCode.csproj", "{EDC37BF4-4908-49F2-B5DB-601A5B49C716}"
77
EndProject
88
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AddTwoNumbers", "AddTwoNumbers\AddTwoNumbers.csproj", "{3595D1B5-7140-4CC2-AD64-06F74FCFCA98}"
99
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LongestSubstringWithoutRepeating", "LongestSubstringWithoutRepeating\LongestSubstringWithoutRepeating.csproj", "{B69D75B1-D9E9-4292-97C2-60D46AFD87E8}"
11+
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1214
Debug|Any CPU = Debug|Any CPU
@@ -21,8 +23,15 @@ Global
2123
{3595D1B5-7140-4CC2-AD64-06F74FCFCA98}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{3595D1B5-7140-4CC2-AD64-06F74FCFCA98}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{3595D1B5-7140-4CC2-AD64-06F74FCFCA98}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{B69D75B1-D9E9-4292-97C2-60D46AFD87E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{B69D75B1-D9E9-4292-97C2-60D46AFD87E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{B69D75B1-D9E9-4292-97C2-60D46AFD87E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{B69D75B1-D9E9-4292-97C2-60D46AFD87E8}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE
2733
EndGlobalSection
34+
GlobalSection(ExtensibilityGlobals) = postSolution
35+
SolutionGuid = {1EEF180B-546B-4184-9AA6-753919570D5E}
36+
EndGlobalSection
2837
EndGlobal
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Xml.XPath;
4+
5+
namespace LongestSubstringWithoutRepeating
6+
{
7+
class Program
8+
{
9+
static void Main ( string[] args )
10+
{
11+
var input = new Dictionary<string,int>()
12+
{
13+
{"abcabcbb",3},
14+
{"bbbbbbbb",1 },
15+
{"pwwkew", 3 },
16+
{"",0 },
17+
{" ",1 },
18+
{"au",2 },
19+
{"dvdf",3}
20+
};
21+
22+
foreach (var pair in input)
23+
{
24+
var result = CheckTheLongestSubstring(pair.Key);
25+
Console.WriteLine($"{pair.Key}({pair.Value}): {result}");
26+
}
27+
28+
Console.WriteLine("Press ENTER to exit...");
29+
Console.ReadLine();
30+
31+
}
32+
33+
static int CheckTheLongestSubstring ( string s)
34+
{
35+
if(string.IsNullOrEmpty(s))
36+
{
37+
return 0;
38+
}
39+
40+
if(s.Length == 1)
41+
{
42+
return 1;
43+
}
44+
45+
var acturlResult = -1;
46+
var j = 0;
47+
var hash = new Dictionary<char,int>();
48+
49+
while(j < s.Length)
50+
{
51+
var current = s[j];
52+
if(hash.ContainsKey(current))
53+
{
54+
j = hash[current] + 1;
55+
acturlResult = Math.Max(hash.Count,acturlResult);
56+
hash.Clear();
57+
}
58+
hash.Add(s[j],j);
59+
j++;
60+
}
61+
return hash.Count == 0 ? acturlResult : Math.Max(acturlResult,hash.Count);
62+
}
63+
64+
}
65+
}

0 commit comments

Comments
 (0)