Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- var input = "abcdeabcdebb";
- var output = lengthOfLongestSubstring(input);
- Console.WriteLine(output);
- input = "bbbbaaa";
- output = lengthOfLongestSubstring(input);
- Console.WriteLine(output);
- Console.ReadLine();
- // The longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3.
- // for "bbbbb" the longest substring "b" with the length of 1.
- }
- static int lengthOfLongestSubstring(string s)
- {
- int i = 0, j = 0;
- int maxLen = 0;
- // Set all characters as not-existing
- var exist = new bool[256];
- while (j < s.Length) // Get the length of string
- {
- // Check if the character exists
- if (exist[s[j]])
- {
- maxLen = Math.Max(maxLen, j - i);
- while (s[i] != s[j])
- {
- exist[s[i]] = false;
- i++;
- }
- i++;
- j++;
- }
- else
- {
- exist[s[j]] = true;
- j++;
- }
- }
- return maxLen = Math.Max(maxLen, s.Length - i);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement