Day8-9
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_3._Longest_Substring_Without_Repeating_Characters</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace _3._Longest_Substring_Without_Repeating_Characters;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine(new Solution().LengthOfLongestSubstring("dvdf"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace _3._Longest_Substring_Without_Repeating_Characters;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public int LengthOfLongestSubstring(string s)
|
||||
{
|
||||
int max = 0;
|
||||
Dictionary<char, int> dict = new Dictionary<char, int>();
|
||||
for(int i = 0; i < s.Length; i++)
|
||||
{
|
||||
char c = s[i];
|
||||
if(dict.ContainsKey(c))
|
||||
{
|
||||
max = Math.Max(max, dict.Count);
|
||||
i = dict[c];
|
||||
dict.Clear();
|
||||
}
|
||||
else
|
||||
dict.Add(c, i);
|
||||
}
|
||||
max = Math.Max(max, dict.Count);
|
||||
return max;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user