{"id":22513704,"url":"https://github.com/emahtab/longest-substring-without-repeating-characters","last_synced_at":"2026-03-19T23:03:08.127Z","repository":{"id":79525610,"uuid":"264330609","full_name":"eMahtab/longest-substring-without-repeating-characters","owner":"eMahtab","description":"Longest Substring Without Repeating Characters","archived":false,"fork":false,"pushed_at":"2025-01-18T01:49:37.000Z","size":19,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-09T08:08:15.085Z","etag":null,"topics":["hashset","leetcode","sliding-window"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-05-16T00:53:46.000Z","updated_at":"2025-01-18T01:49:38.000Z","dependencies_parsed_at":"2025-01-18T02:45:57.443Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/longest-substring-without-repeating-characters","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/longest-substring-without-repeating-characters","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flongest-substring-without-repeating-characters","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flongest-substring-without-repeating-characters/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flongest-substring-without-repeating-characters/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flongest-substring-without-repeating-characters/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/longest-substring-without-repeating-characters/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Flongest-substring-without-repeating-characters/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28937815,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-31T08:53:31.997Z","status":"ssl_error","status_checked_at":"2026-01-31T08:51:38.521Z","response_time":128,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hashset","leetcode","sliding-window"],"created_at":"2024-12-07T03:14:07.959Z","updated_at":"2026-01-31T10:02:45.878Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Longest Substring Without Repeating Characters\n## https://leetcode.com/problems/longest-substring-without-repeating-characters\n\nGiven a string, find the length of the longest substring without repeating characters.\n```\nExample 1:\n\nInput: \"abcabcbb\"\nOutput: 3 \nExplanation: The answer is \"abc\", with the length of 3. \n\nExample 2:\n\nInput: \"bbbbb\"\nOutput: 1\nExplanation: The answer is \"b\", with the length of 1.\n\nExample 3:\n\nInput: \"pwwkew\"\nOutput: 3\nExplanation: The answer is \"wke\", with the length of 3. \n             Note that the answer must be a substring, \"pwke\" is a subsequence and not a substring.\n```\n# Implementation 1 : Naive O(n^3)\n```java\nclass Solution {\n    public int lengthOfLongestSubstring(String s) {\n        int longest = 0;\n        for(int i = 0; i \u003c s.length(); i++) {\n            for(int j = i+1; j \u003c s.length() + 1; j++) {\n                String substr = s.substring(i, j);\n                longest = Math.max(longest, check(substr));\n            }\n        }\n        return longest;\n    }\n\n    private int check(String str) {\n        Set\u003cCharacter\u003e set = new HashSet\u003c\u003e();\n        int length = 0;\n        for(char ch : str.toCharArray()) {\n            if(set.contains(ch))\n              return length;\n            length++;\n            set.add(ch);  \n        }\n        return length;\n    }\n}\n```\n#### Sliding Window Approach :\n1. The idea is to keep moving right pointer and add characters to the hashset as long as we haven't seen it before.\n\n2. But if we are at a character `ch` which is already present in the hashset, then it means that current character `ch` is the repeatation of a `previous occurrence` of the same character `ch`, In this case we keep removing elements in the hashset from the left and increment left pointer, we do this until we get rid of the character `ch` from the hashset. By doing this we guarantee that substring between left and right pointer does not contain any duplicate characters. And since we remove the earlier occurrence of `ch` from the hashset, we can count the current `ch` as distinct, so we add it to the hashset.  \n\n# Implementation 2 : Sliding Window, Using HashSet,  O(n)\n```java\nclass Solution {\n    public int lengthOfLongestSubstring(String s) {\n       if(s == null || s.length() == 0)\n         return 0;\n       int i = 0, j = 0, max = 0;\n       Set\u003cCharacter\u003e set = new HashSet\u003c\u003e();\n       while(i \u003c s.length()) {\n         char ch = s.charAt(i);\n         while(set.contains(ch)) {\n            set.remove(s.charAt(j));\n            j++;\n         }\n         set.add(ch);\n         max = Math.max(max, i-j+1);\n         i++;\n       }\n       return max;  \n    }\n}\n```\n## Implementation 2a : Sliding Window, Using HashMap, O(n)\n```java\nclass Solution {\n    public int lengthOfLongestSubstring(String s) {\n        if(s.length() \u003c 2)\n           return s.length();\n\n        Map\u003cCharacter,Integer\u003e map = new HashMap\u003c\u003e();\n        int left = 0;\n        int right = 0;\n        int longestSubstringLength = 1;\n        while(right \u003c s.length()) {\n            char ch = s.charAt(right);\n            if(map.containsKey(ch)) {\n                while(map.containsKey(ch)) {\n                    char charAtLeft = s.charAt(left);\n                    map.remove(charAtLeft);\n                    left++;\n                }\n            }\n            map.put(ch, right);\n            int substringLength = right - left + 1;\n            longestSubstringLength = Math.max(longestSubstringLength, substringLength);\n            right++;\n        }   \n        return longestSubstringLength;\n    }\n}\n```\n\n# References :\n1. https://leetcode.com/articles/longest-substring-without-repeating-characters\n2. https://www.youtube.com/watch?v=4i6-9IzQHwo\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Flongest-substring-without-repeating-characters","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Flongest-substring-without-repeating-characters","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Flongest-substring-without-repeating-characters/lists"}