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

0% completed

Vote For New Content
Anything wrong with this solution? Thanksstatic int longestSubstring(String inpu...

Sukumar

Jul 1, 2022

Anything wrong with this solution? Thanks static int longestSubstring(String input) { int maxReturnCount = 0; int windowStart = 0; Set store = new HashSet(); for(int i = 0; i < input.length(); i++) { char currentChar = input.charAt(i); if(store.contains(currentChar)) { store = new HashSet(); windowStart = i; } store.add(currentChar); maxReturnCount = Math.max(maxReturnCount, i - windowStart + 1); } return maxReturnCount; }

0

0

Comments
Comments
P
Panda 3 years ago

if right_char in char_index_map:

this is tricky; in the current window, we will not have any 'right_char' after

its previous index and if 'window_start' is already ahead of the last index of

'right_char', we'll keep 'window_start'

window_start = max(window_start, ...

K
K Curtis3 years ago

Similar to the solution I came up with:

public static int lsub(String str) { int longest = 0; int windowStart = 0; HashSet cf = new HashSet(); for(int windowEnd = 0; windowEnd < str.length(); windowEnd++) { while(cf.size() > 0 && cf.contains(str.charAt(windowEnd))) { ...

On this page