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

0% completed

Vote For New Content
A less tricky solution would be to check if the frequency of the current charact...

Clodoaldo Favaro

Sep 8, 2022

A less tricky solution would be to check if the frequency of the current character is greater than 1, and if it is, we slide our left pointer to the right until the frequency of the current char is not greater than 1.

function longestSubAllDistinctChar(str) { let freq = {}; let start = 0; let longest = 0;

for (let end = 0; end < str.length; end++) { const rChar = str[end]; if (!(rChar in freq)) { freq[rChar] = 0; } freq[rChar]++;

while (freq[rChar] > 1) { const lChar = str[start]; freq[lChar]--; start++; } longest = Math.max(longest, end - start + 1); }

return longest; }

0

0

Comments
Comments

On this page