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 :
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.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework