
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to find the length of the longest substring from the given string without repeating the characters using C#?
From the given string input, use the sliding window technique by having 2 pointers i and j . both i and j will point to the same character in the string. Traverse through the string and add it to the list. If the repeated character is found then remove it from the list else append to the list.
Example 1
Input − s = "abcabcbb"
Output − 3
Explanation − The answer is "abc", with the length of 3.
Example 2
Input − s = "bbbbb"
Output − 1
Explanation − The answer is "b", with the length of 1.
Time complexity − O(N)
Space complexity − O(N)
Example
public class Arrays{ public int LongestSubstringWithNoRepeatingCharacters(string s){ List<char> c = new List<char>(); int iPointer = 0; int jpointer = 0; int max = 0; while (jpointer < s.Length){ if (c.Contains(s[jpointer])){ c.Remove(s[iPointer]); iPointer++; } else{ max = Math.Max(c.Count(), max); c.Add(s[jpointer]); jpointer++; } } return max; } } static void Main(string[] args){ int res = s.LongestSubstringWithNoRepeatingCharacters("abcabcbb"); Console.WriteLine(res); }
Output
2
- Related Articles
- Longest Substring Without Repeating Characters in Python
- Program to find length of longest repeating substring in a string in Python
- Find length of the longest consecutive path from a given starting characters in C++
- Find the longest substring with k unique characters in a given string in Python
- Longest Substring with At Least K Repeating Characters in C++
- Finding the longest Substring with At Least n Repeating Characters in JavaScript
- Longest Repeating Substring in C++
- Finding the length of longest vowel substring in a string using JavaScript
- How to find the length of the longest continuous increasing subsequence from an array of numbers using C#?
- Program to find length of longest substring which contains k distinct characters in Python
- Program to find length of longest common substring in C++
- How to find the longest distance to a character in a given string using C#?
- Find length of longest subsequence of one string which is substring of another string in C++
- Program to print the longest common substring using C++
- Find the Length of the Longest possible palindrome string JavaScript

Advertisements