Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
how about this solution, basically once we encounter a duplicate then just clea...

ag

Sep 6, 2022

how about this solution, basically once we encounter a duplicate then just clear the set

int windowStart = 0, maxLength = 0; Set cache = new HashSet(); for (int windowEnd = 0; windowEnd < str.length(); windowEnd++) { char c = str.charAt(windowEnd); if (cache.contains(c)) { cache.clear(); windowStart = windowEnd; } cache.add(c); maxLength = Math.max(maxLength, windowEnd - windowStart + 1); }

return maxLength;

2

0

Comments
Comments
S
Salah Osman3 years ago

This is basically what I did O(n) time O(n) space

On this page