top of page
  • LinkedIn
  • Facebook
  • YouTube
  • Twitter
  • Instagram
  • Pinterest
  • Tumblr
  • Vkontakte

Longest Substring Without Repeating Characters - Leetcode #3 Short & Simple Solution

Updated: Jun 3, 2022


Problem Statement


In our previous article we solved the two sum problem. This is another article in the series leetcode problem solutions and this article is a solution to leetcode 3 problem.


For a given input string s, return the length of the longest substring in s without repeating characters. s consists of English letters and digits.


Example


Example 1:

Input: s = "abcabcbb"
Output: 3

Example 2:

Input: s = "bbbbb"
Output: 1

Example 3:

Input: s = "pwwkew"
Output: 3

Solution


Let us try to understand the problem statement first. This is a pretty straightforward problem if you know what a substring is for a given string.


A substring a continuous sequence of characters in a given string. For example, the substrings of string "abcd" are: "a", "ab", "abc", "abcd", "b", "bc", "bcd", "c", "cd" and "d". If the characters are not continuous it is not considered a substring. "bd", "ad", "acd" etc. are not substrings because the characters are not continuous as compared to the original string s.


Note: Substring is different from a subsequence which may not be consecutive in nature.


Now that we know what a substring is, we need to write an algorithm t