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

0% completed

Vote For New Content
Go based solutionfunc findPermutation(input, pattern string) bool { winStart := ...

Bipra

Feb 13, 2022

Go based solution

func findPermutation(input, pattern string) bool {

winStart := 0 charFreq := make(map[string]int, len(pattern)) for _, v := range pattern { charFreq[string(v)]++ } for winEnd := 0; winEnd < len(input); winEnd++ { if _, ok := charFreq[string(input[winEnd])]; !ok { for winStart

0

0

Comments
Comments
F
First 4 years ago

Why do we use if (windowEnd >= pattern.length - 1) instead of if(windowEnd - windowStart + 1 >= pattern.length-1)

Design Gurus
Design Gurus4 years ago

Both conditions will work.

The given condition works because once the windowEnd reaches the pattern length, we slide the window by one character, that is, increment windowStart by one and decrement windowEnd by one.

F
First 4 years ago

thanks!

Design Gurus
Design Gurus4 years ago

Typo in the above statement: increment both windowStart and windowEnd by one.

D
Dee 3 years ago

If both will work, why does only (windowEnd >= pattern.length - 1) work for me instead both (windowEnd >= pattern.length - 1) and if(windowEnd - windowStart + 1 >= pattern.length-1) ?

On this page