Leetcode

00003. Leetcode: Longest Substring Without Repeating Characters – Problem and Solution

Lets check out the third problem listed on the Leetcode website!

Problem Analysis

The task is to find the length of the longest substring without any repeating characters. This requires iterating through the string, keeping track of the current substring and its length, and updating the length when a repeating character is encountered.

Approach

One common approach to solve this problem is to use the sliding window technique. Maintain two pointers (start and end) that define the current substring. Move the end pointer to include new characters, and if a repeating character is found, move the start pointer to the right until the substring becomes valid again. Keep track of the maximum length encountered during this process.

Solution

Solution
 public int LengthOfLongestSubstring(string s)
    {
        int n = s.Length;
        int maxLength = 0;
        int start = 0;
        Dictionary<char, int> charIndexMap = new Dictionary<char, int>();

        for (int end = 0; end < n; end++)
        {
            if (charIndexMap.ContainsKey(s[end]) && charIndexMap[s[end]] >= start)
            {
                // If the character is repeated, update the start pointer to the right
                start = charIndexMap[s[end]] + 1;
            }

            charIndexMap[s[end]] = end;
            maxLength = Math.Max(maxLength, end - start + 1);
        }

        return maxLength;
    }

This LengthOfLongestSubstring method takes a string s as input and returns the length of the longest substring without repeating characters.

Here’s a brief explanation of the key parts of the code:

  • We use two pointers, start and end, to define the current substring.
  • The charIndexMap dictionary is used to store the index of each character’s last occurrence in the string.
  • We iterate through the string, updating the start pointer when a repeating character is encountered.
  • We update the maxLength variable with the length of the current valid substring.

This solution has a time complexity of O(n), where n is the length of the input string s, making it an efficient approach for this problem.

Conclusion

Solving the Leetcode problem “Longest Substring Without Repeating Characters” problem involves efficiently traversing the string and maintaining a substring without any repeating characters. The sliding window approach is a popular and efficient strategy for solving such substring-related problems.

Danilo Cavalcante

Working with web development since 2005, currently as a senior programmer analyst. Development, maintenance, and integration of systems in C#, ASP.Net, ASP.Net MVC, .Net Core, Web API, WebService, Integrations (SOAP and REST), Object-Oriented Programming, DDD, SQL, Git, and JavaScript

View Comments

  • Its like you read my mind! You seem to know so much about this,
    like you wrote the book in it or something.
    I think that you can do with some pics to drive the message home a bit,
    but other than that, this is excellent blog. An excellent read.
    I'll certainly be back.

  • Hello There. I found your blog using msn. This is an extremely well written article.

    I will be sure to bookmark it and come back to read more
    of your useful info. Thanks for the post. I will definitely comeback.

Recent Posts

Encapsulation and Abstraction in C#

Encapsulation and abstraction are two pillars of object-oriented programming (OOP) that play a vital role…

7 days ago

Polymorphism in C#: Object-Oriented Programming

Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects to take on…

1 week ago

Understanding Inheritance in C#

Inheritance is a cornerstone of object-oriented programming (OOP) and one of its most powerful features.…

2 weeks ago

Classes and Objects in C#: Object-Oriented Programming

In the world of C# and object-oriented programming (OOP), classes and objects form the backbone…

2 weeks ago

Collections and LINQ Queries in C#

In modern C# programming, working with data collections is a common task. Understanding how to…

2 weeks ago

Exception Handling in C#: try-catch, finally, and Custom Exceptions

Exception handling is a critical part of writing robust and maintainable C# applications. It allows…

2 weeks ago