01 Jan 2021 | New-year,-new-problems

This took me a bit longer than it probably should have because of subtle issues I didn’t foresee that the test cases definitely test for. All in all though, not too tricky. These harder problems aren’t so bad, really.

Longest Substring Without Repeating Characters

Difficulty

Medium

class Solution {
    public int lengthOfLongestSubstring(String s) {
        List<Character> acc = new ArrayList<>();
        int longest = 0;
        for (char x : s.toCharArray()) {
            if (acc.contains(x)) {
                acc = acc.subList(acc.indexOf(x) + 1, acc.size());
            }
            acc.add(x);
            if (acc.size() > longest) {
                longest = acc.size();
            }
        }
        return longest;
    }
}

Submission Details

Runtime: 1640 ms

Memory Usage: 39 MB