Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

longest substring without repeating characters c#

public class Solution 
{
    public int LengthOfLongestSubstring(string str) 
    {
        var dict = new Dictionary<char, int>();
        var max = 0;
        int start = 0;
        for (int i = 0; i < str.Length; i++)
        {
            var x = str[i];
            if (!dict.ContainsKey(x))
            {
                dict.Add(x, 1);
            }
            else
            {
                dict[x] += 1;
                while (start <= i && dict.ContainsKey(str[start]) && dict[x] > 1)
                {
                    dict[str[start]]--;
                    if (dict[str[start]] == 0)
                        dict.Remove(str[start]);
                    start++;
                }
            }
            max = Math.Max(max, i - start + 1);
        }
        return max;
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: concatenation in c# 
Csharp :: get min date in list c# 
Csharp :: wpf keyboard press event 
Csharp :: unity detect when an object has been clicked 
Csharp :: how to get mouse position c# 
Csharp :: ontriggerenter 
Csharp :: c sharp 
Csharp :: C# loop through the registry searching for keys containing 
Csharp :: max index array c# 
Csharp :: div element position in screen 
Csharp :: count the number of notes in a given amount c# 
Csharp :: c# check if object is of any generic type 
Csharp :: Get all images from folder asp.net 
Csharp :: c# compare dateTime with string 
Csharp :: constructor in c# 
Csharp :: how to change text in richtextbox wpf 
Csharp :: c# instance class with ilogger 
Csharp :: update browserslist 
Csharp :: monogame print debug 
Csharp :: how to serialize a property in unity 
Csharp :: cache trong mvc 
Csharp :: how to make a character jump c# 
Csharp :: administrative priviledge in c# 
Csharp :: disable alt + f4 in c# forms 
Csharp :: Reading emails from Gmail in C# 
Csharp :: length of list c# 
Csharp :: Test for even Number 
Csharp :: linq contains 
Csharp :: unity3d gameobject follow path 
Csharp :: instantiate date time variable C# 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =