// JavaScript Solution - Credit to @albertchanged on LC
// Sliding window implementation
var lengthOfLongestSubstring = function(s) {
if (!s.length) return 0;
let left = 0, right = 0;
let maxLength = -Infinity;
const set = new Set();
while (right < s.length) {
// If s[right] has not been seen yet
if (!set.has(s[right])) {
// Add it to the set
set.add(s[right]);
// Increase size of window to right
right++;
// Update maxLength; set size represents length of unique substring
maxLength = Math.max(maxLength, set.size);
} else {
// We've seen s[right] so we need to shrink the window
// Delete s[left] from set
set.delete(s[left]);
// Shrink window from left
left++;
}
}
return maxLength;
}
public class Solution
{
public int LengthOfLongestSubstring(string str)
{
var dict = new Dictionary<char, int>();
var max = 0;
int start = 0;
for (int i = 0; i < str.Length; i++)
{
var x = str[i];
if (!dict.ContainsKey(x))
{
dict.Add(x, 1);
}
else
{
dict[x] += 1;
while (start <= i && dict.ContainsKey(str[start]) && dict[x] > 1)
{
dict[str[start]]--;
if (dict[str[start]] == 0)
dict.Remove(str[start]);
start++;
}
}
max = Math.Max(max, i - start + 1);
}
return max;
}
}
import java.io.*;
class GFG {
public static int longestUniqueSubsttr(String str)
{
String test = "";
// Result
int maxLength = -1;
// Return zero if string is empty
if (str.isEmpty()) {
return 0;
}
// Return one if string length is one
else if (str.length() == 1) {
return 1;
}
for (char c : str.toCharArray()) {
String current = String.valueOf(c);
// If string already contains the character
// Then substring after repeating character
if (test.contains(current)) {
test = test.substring(test.indexOf(current)
+ 1);
}
test = test + String.valueOf(c);
maxLength = Math.max(test.length(), maxLength);
}
return maxLength;
}
// Driver code
public static void main(String[] args)
{
String str = "geeksforgeeks";
System.out.println("The input string is " + str);
int len = longestUniqueSubsttr(str);
System.out.println("The length of the longest "
+ "non-repeating character "
+ "substring is " + len);
}
}
// This code is contributed by Alex Bennet
find longest substring without repeating characters
One thing needs to be mentioned is that when asked to find maximum substring, we should update maximum after the inner while loop to guarantee that the substring is valid. On the other hand, when asked to find minimum substring, we should update minimum inside the inner while loop.
The code of solving Longest Substring with At Most Two Distinct Characters is below:
int lengthOfLongestSubstringTwoDistinct(string s) {
vector<int> map(128, 0);
int counter=0, begin=0, end=0, d=0;
while(end<s.size()){
if(map[s[end++]]++==0) counter++;
while(counter>2) if(map[s[begin++]]--==1) counter--;
d=max(d, end-begin);
}
return d;
}
The code of solving Longest Substring Without Repeating Characters is below:
Update 01.04.2016, thanks @weiyi3 for advise.
int lengthOfLongestSubstring(string s) {
vector<int> map(128,0);
int counter=0, begin=0, end=0, d=0;
while(end<s.size()){
if(map[s[end++]]++>0) counter++;
while(counter>0) if(map[s[begin++]]-->1) counter--;
d=max(d, end-begin); //while valid, update d
}
return d;
}
I think this post deserves some upvotes! : )