Gidhub BE Developer

LeetCode : 3. Longest Substring Without Repeating Characters

2021-03-21
goodGid

3. Longest Substring Without Repeating Characters

Problem

Given a string s, find the length of the longest substring without repeating characters.

Example

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

[1] Code (21. 03. 21)

class Solution {
    public int lengthOfLongestSubstring(String s) {
        
        Set<Character> set = new HashSet<>();
        
        int l = 0;
        int r = 0;
        int ans = 0;
        
        while (r <s.length()){
            if (set.contains(s.charAt(r))){
                set.remove(s.charAt(l));
                l++;
            } else {
                set.add(s.charAt(r));
                ans = Math.max(ans, r-l+1);
                r++;
            }
        }       
        return ans;        
    }
}

Algorithm Description

  • 2 Pointer
left와 right를 사용

right가 움직일 때
Set에 해당하는 char가 존재하면
left를 오른쪽으로 이동시켰다.
그와 동시에 set에서 제거를 해주었다.

Review

  • 무난했던 문제

Reference


Comments

Index