본문 바로가기
Algorithm

[Algorithm][Array] 문제 풀이 #12 - Longest Substring Without Repeating Characters

by Lee David 2022. 7. 24.
반응형
문제 링크

https://leetcode.com/problems/longest-substring-without-repeating-characters/

내 코드
public int lengthOfLongestSubstring(String s) {
    int result = 0;
    Integer[] alphabets = new Integer[128];

    int left = 0;
    int right = 0;
    while (right < s.length()) {
        char c = s.charAt(right);
        Integer index = alphabets[c];
        if(index != null && left <= index && index < right) {
            left = index + 1;
        }

        result = Math.max(result, right - left + 1); 

        alphabets[c] = right;
        right++;
    }

    return result;
}
결과

예전에 풀어봤던 문제였지만 역시나 기억이 나질 않는다...

풀었던 풀이를 그대로 다시 배껴서 리마인드 형식으로 문제를 풀고나니 예전보다 조금은 이해도가 높아진(?) 기분이 든다.

반응형