Given a string S, find the length of the longest substring without repeating characters. 
Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
 
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring. 
 
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
Constraints:
1 ≤ |S| ≤ 10^{5}
It is guaranteed that all characters of the String S will be lowercase letters from 'a' to 'z'

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: String algorithms

Data structures

Amortized analysis

Starter code: #User function Template for python3

class Solution:
    def longestUniqueSubsttr(self, S):
        # code here

Solutions:
class Solution:

	def longestUniqueSubsttr(self, string):
		seen = {}
		maximum_length = 0
		start = 0
		for end in range(len(string)):
			if string[end] in seen:
				start = max(start, seen[string[end]] + 1)
			seen[string[end]] = end
			maximum_length = max(maximum_length, end - start + 1)
		return maximum_length


class Solution:

	def longestUniqueSubsttr(self, S):
		n = len(S)
		d = {}
		for i in range(n):
			d[S[i]] = -1
		d[S[0]] = 0
		l = 1
		maxx = 1
		for i in range(1, n):
			x = d[S[i]]
			if x == -1 or i - l > x:
				l += 1
			else:
				maxx = max(maxx, l)
				l = i - x
			d[S[i]] = i
		maxx = max(maxx, l)
		return maxx


class Solution:

	def longestUniqueSubsttr(self, S):
		n = len(S)
		cur_len = 1
		max_len = 1
		prev_index = -1
		visited = {}
		for i in S:
			visited[i] = -1
		visited[S[0]] = 0
		for i in range(1, n):
			prev_index = visited[S[i]]
			if prev_index == -1 or i - cur_len > prev_index:
				cur_len += 1
			else:
				if cur_len > max_len:
					max_len = cur_len
				cur_len = i - prev_index
			visited[S[i]] = i
		if cur_len > max_len:
			max_len = cur_len
		return max_len


class Solution:

	def longestUniqueSubsttr(self, S):
		if len(S) == 1 or S == S[0] * len(S):
			return 1
		start = 0
		l = 0
		i = 1
		while i < len(S):
			if S[i] in S[start:i]:
				while start < i:
					if S[start] == S[i]:
						start += 1
						break
					start += 1
			l = max(l, len(S[start:i + 1]))
			i += 1
		return l


class Solution:

	def longestUniqueSubsttr(self, S):
		j = 0
		i = 0
		x = {}
		max_len = 0
		while j < len(S):
			if S[j] not in x:
				x[S[j]] = 1
			else:
				max_len = max(max_len, j - i)
				while i < j and S[i] != S[j]:
					del x[S[i]]
					i += 1
				del x[S[i]]
				i += 1
				x[S[j]] = 1
			j += 1
		max_len = max(max_len, j - i)
		return max_len


class Solution:

	def longestUniqueSubsttr(self, S):
		if len(S) == 0:
			return 0
		max_ans = float('-inf')
		setx = set()
		l = 0
		for r in range(len(S)):
			if S[r] in setx:
				while l < r and S[r] in setx:
					setx.remove(S[l])
					l += 1
			setx.add(S[r])
			max_ans = max(max_ans, r - l + 1)
		return max_ans


class Solution:

	def longestUniqueSubsttr(self, s):
		temp = ''
		ans = 0
		for i in range(len(s)):
			ch = s[i]
			if ch not in temp:
				temp += ch
				ans = max(ans, len(temp))
			else:
				temp += ch
				while temp[0] != ch:
					temp = temp[1:]
				temp = temp[1:]
		return ans


class Solution:

	def longestUniqueSubsttr(self, S):
		letters = set()
		l = 0
		res = 0
		for i in range(len(s)):
			while s[i] in letters:
				letters.remove(s[l])
				l += 1
			letters.add(s[i])
			res = max(res, i - l + 1)
		return res


class Solution:

	def longestUniqueSubsttr(self, S):
		start = 0
		end = 0
		maxLen = 0
		charDict = {}
		while end < len(S):
			if S[end] in charDict and charDict[S[end]] >= start:
				start = charDict[S[end]] + 1
			charDict[S[end]] = end
			end += 1
			maxLen = max(maxLen, end - start)
		return maxLen


class Solution:

	def longestUniqueSubsttr(self, S):
		max_len = float('-inf')
		d = {}
		(i, j) = (0, 0)
		while i < len(S):
			if S[i] not in d:
				d[S[i]] = 1
				max_len = max(max_len, i - j + 1)
				i += 1
			else:
				while S[i] in d:
					del d[S[j]]
					j += 1
		return max_len


from collections import deque

class Solution:

	def longestUniqueSubsttr(self, S):
		lis = deque()
		le = 0
		mx = 0
		for i in range(len(S)):
			if S[i] not in lis:
				lis.append(S[i])
				le += 1
				mx = max(mx, le)
			else:
				t = lis.popleft()
				l = 0
				while t != S[i]:
					l += 1
					t = lis.popleft()
				le -= l
				lis.append(S[i])
		return mx


class Solution:

	def longestUniqueSubsttr(self, S):
		length = len(S)
		first = 0
		second = 0
		max = 1
		count_list = []
		for i in range(26):
			count_list.append(0)
		while second < length:
			while count_list[ord(S[second]) - ord('a')]:
				count_list[ord(S[first]) - ord('a')] = 0
				first += 1
			count_list[ord(S[second]) - ord('a')] = 1
			count = second - first + 1
			if max < count:
				max = count
			second += 1
		return max


class Solution:

	def longestUniqueSubsttr(self, S):
		length = len(S)
		first = 0
		second = 0
		max_len = 1
		char_dict = {}
		while second < length:
			if S[second] in char_dict:
				first = max(char_dict[S[second]] + 1, first)
			char_dict[S[second]] = second
			max_len = max(max_len, second - first + 1)
			second += 1
		return max_len


class Solution:

	def longestUniqueSubsttr(self, S):
		ans = 0
		l = 0
		A = set()
		for i in range(len(S)):
			while S[i] in A:
				A.remove(S[l])
				l = l + 1
			A.add(S[i])
			ans = max(ans, len(A))
		return ans


class Solution:

	def longestUniqueSubsttr(self, S):
		d = {}
		l = r = 0
		maxlen = 0
		while r < len(S):
			if S[r] in d:
				l = max(d[S[r]] + 1, l)
			d[S[r]] = r
			maxlen = max(maxlen, r - l + 1)
			r += 1
		return maxlen


class Solution:

	def longestUniqueSubsttr(self, S):
		maxi = -10 ** 9
		dic = {}
		start = 0
		for i in range(len(S)):
			if S[i] not in dic:
				dic[S[i]] = i
			else:
				if start < dic[S[i]] + 1:
					start = dic[S[i]] + 1
				dic[S[i]] = i
			maxi = max(maxi, i - start + 1)
		if maxi == -10 ** 9:
			return -1
		return maxi


class Solution:

	def longestUniqueSubsttr(self, S):
		(i, j) = (0, 0)
		maxi = -9999
		n = len(S)
		d = {}
		while j < n:
			if S[j] in d:
				while S[i] != S[j]:
					d[S[i]] -= 1
					if d[S[i]] == 0:
						del d[S[i]]
					i += 1
				i += 1
			elif S[j] not in d:
				d[S[j]] = 1
			if j - i + 1 > maxi:
				maxi = j - i + 1
			j += 1
		return maxi


class Solution:

	def longestUniqueSubsttr(self, S):
		sett = set()
		result = 0
		(leftPtr, rightPtr) = (0, 0)
		for rightPtr in range(len(S)):
			while S[rightPtr] in sett:
				sett.remove(S[leftPtr])
				leftPtr += 1
			sett.add(S[rightPtr])
			result = max(result, len(sett))
		return result


class Solution:

	def longestUniqueSubsttr(self, S):
		hashmap = {}
		left = 0
		right = 0
		i = -1
		j = -1
		max_len = -int(1000000000.0)
		while right < len(S):
			if S[right] not in hashmap:
				hashmap[S[right]] = 1
			else:
				while left <= right and S[right] in hashmap:
					del hashmap[S[left]]
					left += 1
				hashmap[S[right]] = 1
			if i == -1 and j == -1:
				i = left
				j = right
			elif j - i < right - left:
				i = left
				j = right
			max_len = max(max_len, j - i + 1)
			right += 1
		return max_len


class Solution:

	def longestUniqueSubsttr(self, S):
		s = {}
		left = output = 0
		for (right, num) in enumerate(S):
			if num in s:
				left = max(left, s[num] + 1)
			output = max(output, right - left + 1)
			s[num] = right
		return output


class Solution:

	def longestUniqueSubsttr(self, S):
		maxi = float('-inf')
		(i, j) = (0, 0)
		dic = {}
		while i < len(S):
			if S[i] not in dic:
				dic[S[i]] = 1
				maxi = max(maxi, i - j + 1)
				i += 1
			else:
				while S[i] in dic:
					del dic[S[j]]
					j += 1
		return maxi


class Solution:

	def longestUniqueSubsttr(self, S):
		start = end = m = 0
		dic = {}
		for (i, c) in enumerate(S):
			if c in dic and dic[c] >= start:
				m = max(m, end - start)
				start = dic[c] + 1
			dic[c] = i
			end += 1
		return max(m, end - start)


import sys
from collections import defaultdict

class Solution:

	def longestUniqueSubsttr(self, S):
		hmap = defaultdict(int)
		maxi = 0
		left = 0
		for right in range(0, len(S)):
			if S[right] not in hmap:
				hmap[S[right]] = 1
			else:
				hmap[S[right]] += 1
			while hmap[S[right]] > 1:
				hmap[S[left]] -= 1
				left += 1
			maxi = max(maxi, right - left + 1)
		return maxi


class Solution:

	def longestUniqueSubsttr(self, S):
		a = []
		maxm = 0
		if len(S) == 1:
			return 1
		for i in S:
			if i not in a:
				a.append(i)
			else:
				if maxm < len(a):
					maxm = len(a)
				c = a.index(i)
				a = a[c + 1:]
				a.append(i)
		if maxm < len(a):
			return len(a)
		return maxm


from collections import deque

class Solution:

	def longestUniqueSubsttr(self, S):
		max_len = float('-inf')
		dic = dict()
		(i, j) = (0, 0)
		while j < len(S):
			if S[j] not in dic:
				dic[S[j]] = 1
				max_len = max(max_len, j - i + 1)
				j += 1
			else:
				while S[j] in dic:
					del dic[S[i]]
					i += 1
		return max_len


class Solution:

	def longestUniqueSubsttr(self, S):
		n = len(S)
		seen = set()
		left = 0
		right = 0
		max_len = 0
		while left < n and right < n:
			if S[right] not in seen:
				seen.add(s[right])
				right += 1
				max_len = max(max_len, right - left)
			else:
				seen.remove(s[left])
				left += 1
		return max_len


class Solution:

	def longestUniqueSubsttr(self, s):
		char_set = set()
		left = 0
		max_len = 0
		right = 0
		while right < len(s):
			while s[right] in char_set:
				char_set.remove(s[left])
				left += 1
			char_set.add(s[right])
			max_len = max(max_len, right - left + 1)
			right += 1
		return max_len


class Solution:

	def longestUniqueSubsttr(self, S):
		dict = {}
		start = 0
		longest = 0
		for i in range(len(s)):
			if S[i] in dict and dict[S[i]] >= start:
				start = dict[S[i]] + 1
			else:
				longest = max(longest, i - start + 1)
			dict[S[i]] = i
		return longest


class Solution:

	def longestUniqueSubsttr(self, s):
		n = len(s)
		freq = {}
		(l, r) = (0, 0)
		res = 0
		while r < n:
			while s[r] in freq:
				del freq[s[l]]
				l += 1
			freq[s[r]] = 1 + freq.get(s[r], 0)
			res = max(res, r - l + 1)
			r += 1
		return res


class Solution:

	def longestUniqueSubsttr(self, S):
		seen = {}
		ans = 0
		s = 0
		for i in range(len(S)):
			if S[i] in seen:
				s = max(s, seen[S[i]] + 1)
			seen[S[i]] = i
			ans = max(ans, i - s + 1)
		return ans


class Solution:

	def longestUniqueSubsttr(self, S):
		x = set()
		start = 0
		end = 0
		max_l = 0
		while end < len(S):
			if S[end] not in x:
				x.add(S[end])
				end += 1
				max_l = max(max_l, end - start)
			else:
				x.remove(S[start])
				start += 1
		return max_l


class Solution:

	def longestUniqueSubsttr(self, S):
		seen = {}
		mL = float('-inf')
		(start, end) = (0, 0)
		while end < len(S):
			if S[end] in seen:
				start = max(start, seen[S[end]] + 1)
			seen[S[end]] = end
			mL = max(mL, end - start + 1)
			end += 1
		return mL if mL != float('-inf') else 0


class Solution:

	def longestUniqueSubsttr(self, s):
		n = len(s)
		hm = {}
		l = 0
		r = 0
		ans = 0
		while r < n:
			if s[r] in hm:
				l = max(l, hm[s[r]] + 1)
			hm[s[r]] = r
			ans = max(ans, r - l + 1)
			r += 1
		return ans


import math

class Solution:

	def longestUniqueSubsttr(self, s):
		ls = set()
		j = 0
		count = 0
		for i in range(len(s)):
			while s[i] in ls:
				ls.remove(s[j])
				j += 1
			ls.add(s[i])
			count = max(count, len(ls))
		return count


class Solution:

	def longestUniqueSubsttr(self, s):
		if len(s) == 0:
			return 0
		start = 0
		maxl = 0
		d = {}
		for i in range(len(s)):
			if s[i] in d and start <= d[s[i]]:
				start = d[s[i]] + 1
			else:
				maxl = max(maxl, i - start + 1)
			d[s[i]] = i
		return maxl


class Solution:

	def longestUniqueSubsttr(self, S):
		i = -1
		j = -1
		mlen = 0
		hashmap = {}
		le = len(S) - 1
		while True:
			f1 = False
			f2 = False
			while i < le:
				f1 = True
				i += 1
				ch = S[i]
				if ch in hashmap:
					hashmap[ch] += 1
				else:
					hashmap[ch] = 1
				if hashmap[ch] == 2:
					break
				else:
					clen = i - j
					if clen > mlen:
						mlen = clen
			while j < i:
				f2 = True
				j += 1
				ch = S[j]
				hashmap[ch] -= 1
				if hashmap[ch] == 1:
					break
			if f1 == False and f2 == False:
				break
		return mlen


class Solution:

	def longestUniqueSubsttr(self, S):
		l = []
		s1 = ''
		for i in S:
			if i not in s1:
				s1 += i
			else:
				l.append(s1)
				k = s1.index(i)
				s1 = s1[k + 1:] + i
		l.append(s1)
		l.sort(key=len)
		return len(l[-1])


from queue import Queue

class Solution:

	def longestUniqueSubsttr(self, S):
		if len(S) > 0:
			memo = []
			i = 0
			memo.append(S[i])
			count = 1
		else:
			count = 0
		counter = []
		for i in range(1, len(S)):
			if S[i] in memo:
				counter.append(count)
				current = memo.pop(0)
				while current != S[i]:
					current = memo.pop(0)
				memo.append(S[i])
				count = len(memo)
			else:
				count += 1
				memo.append(S[i])
		counter.append(count)
		return max(counter)


class Solution:

	def longestUniqueSubsttr(self, S):
		hashSet = set()
		l = 0
		r = 0
		n = len(S)
		maxLen = -1
		while r < n:
			while S[r] in hashSet:
				hashSet.remove(S[l])
				l += 1
			hashSet.add(S[r])
			maxLen = max(maxLen, r - l + 1)
			r += 1
		return maxLen


class Solution:

	def longestUniqueSubsttr(self, s):
		n = len(s)
		ans = 0
		mem = {}
		l = 0
		for i in range(n):
			if s[i] not in mem.keys():
				mem[s[i]] = i
			else:
				ans = max(ans, i - l)
				while l < mem[s[i]]:
					del mem[s[l]]
					l += 1
				l += 1
				mem[s[i]] = i
		if n - l > ans:
			ans = n - l
		return ans


class Solution:

	def longestUniqueSubsttr(self, s):
		d = {}
		l = 0
		ml = 0
		for r in range(len(s)):
			if s[r] not in d:
				ml = max(ml, r - l + 1)
			elif d[s[r]] < l:
				ml = max(ml, r - l + 1)
			else:
				l = d[s[r]] + 1
			d[s[r]] = r
		return ml


class Solution:

	def longestUniqueSubsttr(self, S):
		d = {}
		(i, j, ans) = (0, 0, 0)
		n = len(S)
		while j < n:
			if S[j] not in d:
				d[S[j]] = j
			else:
				ans = max(ans, j - i)
				a = d[S[j]]
				while i <= a:
					del d[S[i]]
					i += 1
				d[S[j]] = j
			j += 1
		ans = max(len(d), ans)
		return ans


class Solution:

	def longestUniqueSubsttr(self, S):
		l = []
		r = []
		if len(set(S)) == len(S):
			return len(S)
		for i in S:
			if i not in r:
				r += [i]
			else:
				l += [len(r)]
				p = i
				while True:
					if p in r:
						r.pop(0)
					else:
						r += [p]
						break
		l += [len(r)]
		return max(l)


class Solution:

	def longestUniqueSubsttr(self, s):
		hashmap = {}
		left = 0
		lengthOfLongestSubstring = 0
		for right in range(len(s)):
			if s[right] in hashmap:
				left = max(hashmap[s[right]] + 1, left)
			hashmap[s[right]] = right
			lengthOfLongestSubstring = max(lengthOfLongestSubstring, right - left + 1)
		return lengthOfLongestSubstring


class Solution:

	def longestUniqueSubsttr(self, arr):
		(r, l) = (0, 0)
		max_len = 0
		count_fr = {}
		while r < len(arr):
			if count_fr.get(arr[r], -1) != -1:
				l = max(count_fr[arr[r]] + 1, l)
			max_len = max(max_len, r - l + 1)
			count_fr[arr[r]] = r
			r += 1
		return max_len


class Solution:

	def longestUniqueSubsttr(self, S):
		max_len = 0
		current_len = 0
		char_set = set()
		left = 0
		right = 0
		while right < len(S):
			if S[right] not in char_set:
				char_set.add(S[right])
				current_len += 1
				max_len = max(max_len, current_len)
				right += 1
			else:
				char_set.remove(S[left])
				left += 1
				current_len -= 1
		return max_len


class Solution:

	def longestUniqueSubsttr(self, s):
		map = {}
		i = max_size = 0
		for (j, num) in enumerate(s):
			if num in map:
				i = max(i, map[num] + 1)
			max_size = max(max_size, j - i + 1)
			map[num] = j
		return max_size


class Solution:

	def longestUniqueSubsttr(self, S):
		dic = {}
		cm = count = i = 0
		while i < len(S):
			if S[i] in dic:
				if cm < count:
					cm = count
				count = 0
				i = dic[S[i]] + 1
				dic = {}
			else:
				dic[S[i]] = i
				count += 1
				i += 1
		if cm > count:
			return cm
		else:
			return count


class Solution:

	def longestUniqueSubsttr(self, S):
		char = [0 for i in range(250)]
		n = len(S)
		j = 0
		ans = 0
		i = 0
		while j < n:
			char[ord(S[j])] += 1
			if char[ord(S[j])] > 1:
				while char[ord(S[j])] > 1:
					char[ord(S[i])] -= 1
					i = i + 1
			ans = max(ans, j - i + 1)
			j = j + 1
		return ans


class Solution:

	def longestUniqueSubsttr(self, S):
		d = {}
		j = 0
		ans = 0
		for i in range(len(s)):
			item = s[i]
			if item in d:
				j = max(j, d[item] + 1)
			d[item] = i
			ans = max(ans, i - j + 1)
		return ans


class Solution:

	def longestUniqueSubsttr(self, s: str) -> int:
		max_len = 0
		(left, right) = (0, 0)
		unique_chars = set()
		while right < len(s):
			if s[right] not in unique_chars:
				unique_chars.add(s[right])
				right += 1
				max_len = max(max_len, right - left)
			else:
				unique_chars.remove(s[left])
				left += 1
		return max_len


class Solution:

	def longestUniqueSubsttr(self, s):
		s_set = set()
		j = 0
		ans = -1
		for i in range(len(s)):
			if s[i] not in s_set:
				s_set.add(s[i])
			else:
				while j < i and s[i] in s_set:
					s_set.remove(s[j])
					j += 1
				s_set.add(s[i])
			ans = max(ans, i - j + 1)
		return ans


class Solution:

	def longestUniqueSubsttr(self, S):
		dict1 = {}
		i = j = 0
		max_len = 0
		while j < len(s):
			if S[j] not in dict1:
				dict1[S[j]] = 1
				max_len = max(max_len, j - i + 1)
				j += 1
			else:
				while S[j] in dict1:
					del dict1[S[i]]
					i += 1
		return max_len


class Solution:

	def longestUniqueSubsttr(self, str1):
		r = 0
		l = 0
		map = {}
		(MxLen, n, lent) = (0, len(str1), 0)
		while r < n:
			if map.get(str1[r], -1) != -1:
				l = max(map[str1[r]] + 1, l)
			map[str1[r]] = r
			MxLen = max(MxLen, r - l + 1)
			r += 1
		return MxLen


class Solution:

	def longestUniqueSubsttr(self, S):
		last_seen = {}
		start = 0
		maxLen = 0
		for i in range(0, len(s)):
			if s[i] in last_seen and last_seen[s[i]] >= start:
				start = last_seen[s[i]] + 1
			last_seen[s[i]] = i
			maxLen = max(maxLen, i - start + 1)
		return maxLen


class Solution:

	def longestUniqueSubsttr(self, S):
		last_seen = {}
		start = 0
		max_length = 0
		for end in range(len(S)):
			if S[end] in last_seen and last_seen[S[end]] >= start:
				start = last_seen[S[end]] + 1
			last_seen[S[end]] = end
			max_length = max(max_length, end - start + 1)
		return max_length


class Solution:

	def longestUniqueSubsttr(self, S):
		has = [-1 for _ in range(256)]
		left = 0
		right = 0
		n = len(S)
		res = 0
		while right < n:
			if has[ord(S[right])] != -1:
				left = max(has[ord(S[right])] + 1, left)
			has[ord(S[right])] = right
			res = max(res, right - left + 1)
			right += 1
		return res


class Solution:

	def longestUniqueSubsttr(self, S):
		n = len(S)
		if n == 0:
			return 0
		res = float('-inf')
		has = set()
		left = 0
		for right in range(n):
			if S[right] in has:
				while left < right and S[right] in has:
					has.remove(S[left])
					left += 1
			has.add(S[right])
			res = max(res, right - left + 1)
		return res


class Solution:

	def longestUniqueSubsttr(self, S):
		d1 = dict()
		ans = 0
		l = 0
		r = 0
		while r < len(S):
			if S[r] in d1:
				l = max(d1[S[r]] + 1, l)
			d1[S[r]] = r
			ans = max(ans, r - l + 1)
			r += 1
		return ans


class Solution:

	def longestUniqueSubsttr(self, s):
		dic = {}
		l = 0
		ans = 0
		for r in range(len(s)):
			if s[r] in dic:
				l = max(l, dic[s[r]] + 1)
			dic[s[r]] = r
			ans = max(ans, r - l + 1)
		return ans


class Solution:

	def longestUniqueSubsttr(self, S):
		mp = {}
		ans = 0
		i = 0
		for j in range(len(S)):
			if S[j] in mp:
				i = max(mp[S[j]], i)
			ans = max(ans, j - i + 1)
			mp[S[j]] = j + 1
		return ans


class Solution:

	def longestUniqueSubsttr(self, S):
		(i, j) = (0, 0)
		fMap = {}
		lenMax = 0
		while j < len(S):
			fMap[S[j]] = fMap.get(S[j], 0) + 1
			while fMap[S[j]] > 1:
				fMap[S[i]] -= 1
				i += 1
			lenMax = max(lenMax, j - i + 1)
			j += 1
		return lenMax


class Solution:

	def longestUniqueSubsttr(self, S):
		i = 0
		j = 0
		freq = {}
		ans = 0
		while j < len(S):
			freq[S[j]] = freq.get(S[j], 0) + 1
			while freq[S[j]] > 1:
				freq[S[i]] -= 1
				i += 1
			ans = max(ans, j - i + 1)
			j += 1
		return ans


class Solution:

	def longestUniqueSubsttr(self, S):
		i = 0
		j = 0
		mp = set()
		ans = 0
		while j < len(S):
			if S[j] not in mp:
				mp.add(S[j])
				count = j - i + 1
				ans = max(count, ans)
				j += 1
			else:
				mp.remove(S[i])
				i += 1
		return ans


class Solution:

	def longestUniqueSubsttr(self, s):
		n = len(s)
		res = 0
		prev = [-1] * 256
		i = 0
		for j in range(n):
			i = max(i, prev[ord(s[j])] + 1)
			maxEnd = j - i + 1
			res = max(res, maxEnd)
			prev[ord(s[j])] = j
		return res


class Solution:

	def longestUniqueSubsttr(self, str):
		l = 0
		res = 0
		a = {}
		arr = []
		for r in range(len(str)):
			while str[r] in a:
				ele = arr.pop(0)
				del a[ele]
				l += 1
			arr.append(str[r])
			a[str[r]] = 1
			res = max(res, r - l + 1)
		return res


class Solution:

	def longestUniqueSubsttr(self, S):
		d = dict()
		(left, ans) = (0, 0)
		for i in range(len(S)):
			if S[i] in d:
				left = max(left, d[S[i]] + 1)
			d[S[i]] = i
			ans = max(ans, i - left + 1)
		return ans


class Solution:

	def longestUniqueSubsttr(self, S):
		l = 0
		r = 0
		max = 0
		a = ''
		for i in s:
			if i in a[l:r]:
				while a[l] != i:
					l += 1
				l += 1
			a += i
			if r - l + 1 > max:
				max = r - l + 1
			r += 1
		return max


class Solution:

	def longestUniqueSubsttr(self, S):
		charset = set()
		l = 0
		res = 0
		for r in range(len(S)):
			while s[r] in charset:
				charset.remove(s[l])
				l += 1
			charset.add(s[r])
			res = max(res, r - l + 1)
		return res