3. Longest Substring Without Repeating Characters | LeetCode | Top Interview 150 | Coding Questions

Published: (January 2, 2026 at 03:57 PM EST)
1 min read
Source: Dev.to

Source: Dev.to

Problem

Solution

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

Related posts

Read more »

1390. Four Divisors

Problem Statement Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such in...