Longest Substring Without Repeating Characters

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

Example 1:

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

Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

Constraints:

  • 0 <= s.length <= 5 * 104
  • s consists of English letters, digits, symbols and spaces.

Solution

Another Sliding Window

This one makes perfect sense!

class Solution {
    public int lengthOfLongestSubstring(String s) {
        var max = 0;
        var freq = new int[128];
        var l = 0;
        var r = 0;

        while (r < s.length()) {
            var c = s.charAt(r);
            freq[c] += 1;

            while (freq[c] > 1) {
                var c2 = s.charAt(l);
                freq[c2] -= 1;
                l += 1;
            }

            max = Math.max(max, r - l + 1);

            r += 1;
        }

        return max;
    }
}

Sliding Window

I don’t really understand this one.

class Solution {
    public int lengthOfLongestSubstring(String s) {
        var l = 0;
        var m = new HashMap<Character, Integer>();
        var answer = 0;
        for (var r = 0; r < s.length(); r++) {
            var c = s.charAt(r);
            if (m.containsKey(c)) {
                l = Math.max(m.get(c), l);
            }
            m.put(c, r + 1);
            answer = Math.max(r - l + 1, answer);
        }
        return answer;
    }
}

Another Unoptimized

class Solution {
    public int lengthOfLongestSubstring(String s) {
        // brute force
        var max = 0;
        for (var l = 0; l < s.length(); l++) {
            for (var r = l; r < s.length(); r++) {
                var freq = new boolean[128];
                var ok = true;
                // check if this string is valid
                for (var i = l; i <= r; i++) {
                    var c = (int) s.charAt(i);
                    if (freq[c]) {
                        ok = false;
                        break;
                    }
                    freq[c] = true;
                }
                if (ok) {
                    max = Math.max(max, r - l + 1);
                }
            }
        }
        return max;
    }
}

Unoptimized

I think this solution is correct, but it hits a time limit exceeded error

class Solution {
    public int lengthOfLongestSubstring(String s) {
        var size = s.length();

        while (size > 0) {
            for (var l = 0; l <= s.length() - size; l++) {
                var r = l + size;
                // check if any of the letters in this range are duplicated
                var set = new HashSet<Character>();
                var ok = true;
                for (var c : s.substring(l, r).toCharArray()) {
                    if (set.contains(c)) {
                        ok = false;
                        break;
                    } else {
                        set.add(c);
                    }
                }
                if (ok) {
                    return size;
                }
            }
            size -= 1;
        }

        return size;
    }
}

Recent posts from blogs that I like

An Introduction to Google’s Approach to AI Agent Security

via Simon Willison

Notes on Cramer's rule

Cramer's rule is a clever solution to the classical system of linear equations Ax=b: \[\begin{bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \\ \end{bmatrix} \begin{bmatrix}x_1 \\ x_2 \\ x_3\end{bmatrix} = \begin{bmatrix}b_1 \\ b_2 \\ b_3\end{bmatrix}\] Usi...

via Eli Bendersky

Brandjes: Paintings as witnesses to fires 1640-1813

Dramatic paintings of towns and cities on fire, usually at night, were popular during the Dutch Golden Age, and known as brandjes. Examples to well into the 19th century.

via The Eclectic Light Company