In this article we will learn about LeetCode Third question Longest Substring Without Repeating Characters in detail with optimized solution with Example.
👉 Given a string s
, find the length of the longest substring without repeating characters.
Table of Contents
Input: s = “abcabcbb”
Output: 3
Explanation: The answer is “abc”, with the length of 3.
Input: s = “bbbbb”
Output: 1
Explanation: The answer is “b”, with the length of 1.
Input: s = “pwwkew”
Output: 3
Explanation: The answer is “wke”, with the length of 3.
Notice that the answer must be a substring, “pwke” is a subsequence and not a substring.
0 <= s.length <= 5 * 10<sup>4</sup>
s
consists of English letters, digits, symbols and spaces.class Solution { public int lengthOfLongestSubstring(String s) { int l=0,r=0; Set<Character> seen = new HashSet(); int max=0; while(r< s.length()){ char c= s.charAt(r); if(seen.add(c)){ max= Math.max(max,r-l+1); r++; }else{ while(s.charAt(l)!=c){ seen.remove(s.charAt(l)); l++; } seen.remove(c); l++; } } return max; } } // 2023
Time complexity: O(N);
Space Complexity: O(1);
I hope you like this Amazing Longest Substring article.🙂
Also Read :
Blockchain is a shared, immutable ledger that facilitates the process of recording transactions and tracking assets…
In this article we will learn about LeetCode Second question Add Two Numbers in detail with optimized…
In this amazing Web Designing Roadmap 2023 article, we’ll learn about A to Z about…
Android developer -- In this amazing Android Developer Roadmap article, we’ll learn about A to…
The roadmap for a full stack developer can vary depending on the individual's goals and…