Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

longest substring without repeating characters leetcode

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 :: c# list add to list 
Csharp :: c# math method to reverse negative or positive 
Csharp :: delete all rows from table linq 
Csharp :: c# round to closest multiple 
Csharp :: wpf datagrid get selected items 
Csharp :: add qtwidgets to cmake file 
Csharp :: where to write fluent api 
Csharp :: c# creating an array 
Csharp :: update table in C# 
Csharp :: csharp csvhelper 
Csharp :: unity audio source 
Csharp :: how to return a value in c# 
Csharp :: c# callback using delegate 
Csharp :: c# array.reduce 
Csharp :: c# get random between 0 and 1 
Csharp :: global exception handler c# 
Csharp :: unity make a gambeobject array 
Csharp :: pubxml environment variables 
Csharp :: null-conditional operators c# 
Csharp :: How to create a Blazor server-side application in command prompt 
Csharp :: get script directory c# 
Csharp :: connect to a database and loop over a recordset in C# 
Csharp :: demand a Security action c# 
Csharp :: Lambda Expression to filter a list of list of items 
Csharp :: string is int f# 
Csharp :: c# networkstream read all bytes 
Csharp :: instantiate an array in c# 
Csharp :: double quotes in a string c# 
Csharp :: convert date to days c# 
Csharp :: C# Async Function with await 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =