본문 바로가기
Problem Solving/LeetCode

[LeetCode] 3. Longest Substring Without Repeating Characters - Java

by graycode 2026. 5. 6.

 문제 링크

 

Longest Substring Without Repeating Characters - LeetCode

Can you solve this real interview question? Longest Substring Without Repeating Characters - Given a string s, find the length of the longest substring without duplicate characters.   Example 1: Input: s = "abcabcbb" Output: 3 Explanation: The answer is "

leetcode.com

 

 풀이 코드

public class Solution {

    public int lengthOfLongestSubstring(String s) {
        boolean[] arr = new boolean[256];
        int r = 0, l = 0, max = 0, n = s.length();
        while (r < n) {
            if (!arr[s.charAt(r)]) {
                arr[s.charAt(r++)] = true;
                max = Math.max(max, r - l);
            } else arr[s.charAt(l++)] = false;
        }

        return max;
    }

}

댓글