-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlidingWindow.MaxNonRepeatingSubstr.20230911.cs
41 lines (36 loc) · 1.39 KB
/
SlidingWindow.MaxNonRepeatingSubstr.20230911.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections;
using System.Collections.Generic;
public class Program
{
// Longest Substring Without Repeating Characters
// Write a function that takes a string and returns the length of the longest substring without repeating characters.
// Example: Input "abcabcbb", Output 3
public static void Main()
{
var longestNonRepeatingSubstringCount = FindLongestNonRepeatingSubstring("abcabcbb");
Console.WriteLine($"Longest non repeating substring count is: {longestNonRepeatingSubstringCount}");
Console.WriteLine("===========================================");
var longestNonRepeatingSubstringCount2 = FindLongestNonRepeatingSubstring("tmmzuxt");
Console.WriteLine($"Longest non repeating substring count is: {longestNonRepeatingSubstringCount2}");
}
public static int FindLongestNonRepeatingSubstring(string theString)
{
var start = 0;
var maximumSubstringCount = 0;
var charsSeenDict = new Dictionary<char, int>();
for(var end = 0; end < theString.Length; end++)
{
var currentChar = theString[end];
if(charsSeenDict.ContainsKey(currentChar))
{
// Char has already been encountered,
start = Math.Max(start, charsSeenDict[currentChar] + 1);
charsSeenDict.Remove(currentChar);
}
maximumSubstringCount = Math.Max(maximumSubstringCount, end - start + 1);
charsSeenDict[currentChar] = end;
}
return maximumSubstringCount;
}
}