Leetcode Longest Substring Without Repeating Characters problem solution in C# programming

In the Leetcode Longest Substring Without Repeating Characters problem solution in C# programming Given a string s, find the length of the longest substring without repeating characters.

Leetcode Longest Substring Without Repeating Characters problem solution in C# programming

public class Solution {
public int LengthOfLongestSubstring(string s) {

        if(s.Length==0)
        {
            return 0;
        }

        HashSet<char> set = new HashSet<char>();
        int count = 0;
        int max = 0;
        for (int i = 0; i < s.Length; i++)
        {
            count = 0;
            for (int j = i; j < s.Length; j++)
            {
                if (!set.Contains(s[j]))
                {
                    set.Add(s[j]);
                    count++;
                }
                else
                {
                    set.Clear();
                    break;
                }
            }
            if(max<count)
            {
                max = count;
            }
        }
        
        return max;
}
}

Also read,

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

Your email address will not be published. Required fields are marked *