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

0% completed

Vote For New Content
An implementation in golang based on maintaining character count instead of char...

Bipra

Feb 7, 2022

An implementation in golang based on maintaining character count instead of character index:

func longestSubStringWithDistinctCharacters(input string) int { charCount := make(map[string]struct{}, len(input)) windowStart := 0 result, length := 0, 0 for windowEnd := 0; windowEnd < len(input); windowEnd++ { ch := string(input[windowEnd]) if _, ok := charCount[ch]; !ok { length++ charCount[ch] = struct{}{} if length >= result { result = length } } else { for windowStart != windowEnd { delete(charCount, string(input[windowStart])) windowStart++ } length = 1 // reset length to 1 charCount[ch] = struct{}{} } } return result }

0

0

Comments
Comments
G
Garima 3 years ago

How about this implementation. it returns correct answer for all inputs mentioned in the problem + others suggested here by people.

const longestSubstringWithDistinctChars = (str) => { let windowStart = 0, maxLength = 0, charFreq = {};

for(let windowEnd = 0; windowEnd...

On this page