Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Find the length of the longest substring without repeating characters from a given string

C# Sharp String: Exercise-47 with Solution

Write a C# Sharp program to find the length of the longest substring without repeating characters from a given string.

Note:
1) Given string consists of English letters, digits, symbols and spaces.
2) 0 <= Given string length <= 5 * 104

Difficulty: Medium.
Company: Amazon, Google, Bloomberg, Microsoft, Adobe, Apple, Oracle, Facebook and more.

Input String : pickoutthelongestsubstring
The longest substring : ubstring
The longest Substring Length : 8

Input String : ppppppppppppp
The longest substring : p
The longest Substring Length : 1

Input String : Microsoft
The longest substring : Micros
The longest Substring Length : 6

Sample Solution:

C# Sharp Code:

using System;
using System.Collections.Generic;
namespace exercises
{
    class Program
   {
       static void Main(string[] args)
       {
           String str1;
           str1="aaaaaabbbbccc";
           Console.WriteLine("Original String: "+str1);
           Console.WriteLine("Length of the longest substring without repeating characters of the said string:");
           Console.WriteLine(without_repeated_chars_longest_substring(str1));
           str1="BDEFGAABEF";
           Console.WriteLine("Original String: "+str1);
           Console.WriteLine("Length of the longest substring without repeating characters of the said string:");;
           Console.WriteLine(without_repeated_chars_longest_substring(str1));
           str1="Python";
           Console.WriteLine("Original String: "+str1);
           Console.WriteLine("Length of the longest substring without repeating characters of the said string:");
           Console.WriteLine(without_repeated_chars_longest_substring(str1));
           str1="Java";
           Console.WriteLine("Original String: "+str1);
           Console.WriteLine("Length of the longest substring without repeating characters of the said string:");
           Console.WriteLine(without_repeated_chars_longest_substring(str1));
       }
    public static int without_repeated_chars_longest_substring(string str1)
       {
             if (string.IsNullOrEmpty(str1)) return 0;
           var map_str = new Dictionary();
           var max_len = 0;
           var last_repeat_pos = -1;
           for (int i = 0; i < str1.Length; i++)
           {
               if (map_str.ContainsKey(str1[i]) && last_repeat_pos < map_str[str1[i]])
                   last_repeat_pos = map_str[str1[i]];
               if (max_len < i - last_repeat_pos)
                   max_len = i - last_repeat_pos;
               map_str[str1[i]] = i;
           }
           return max_len;
       }
 }
}

Sample Output:

Original String: aaaaaabbbbccc
Length of the longest substring without repeating characters of the said string:
2
Original String: BDEFGAABEF
Length of the longest substring without repeating characters of the said string:
6
Original String: Python
Length of the longest substring without repeating characters of the said string:
6
Original String: Java
Length of the longest substring without repeating characters of the said string:
3

Flowchart :

Flowchart: C# Sharp Exercises - Find the length of the longest substring without repeating characters from a given string.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to remove duplicate characters from a given string.
Next: Write a C# Sharp program to reverse the case (upper->lower, lower->upper) of all the characters of given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.