3. 중복 문자 없는 가장 긴 부분 문자열 | LeetCode | Top Interview 150 | Coding Questions

발행: (2026년 1월 3일 오전 05:57 GMT+9)
1 min read
원문: Dev.to

Source: Dev.to

문제

솔루션

class Solution {
    public int lengthOfLongestSubstring(String s) {

        Map map = new HashMap<>();
        int left = 0;
        int maxLength = 0;

        for (int right = 0; right < s.length(); right++) {
            char ch = s.charAt(right);

            if (map.containsKey(ch)) {
                left = Math.max(left, map.get(ch) + 1);
            }

            map.put(ch, right);
            maxLength = Math.max(maxLength, right - left + 1);
        }

        return maxLength;
    }
}
Back to Blog

관련 글

더 보기 »