All files / leetCode 0290.ts

100% Statements 13/13
100% Branches 4/4
100% Functions 1/1
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21  5x 5x   5x 1x   4x 4x   4x 12x 3x   9x 9x     1x    
export default function wordPattern(pattern: string, s: string): boolean {
  const patterns = pattern.split('')
  const words = s.split(' ')
 
  if (words.length !== patterns.length)
    return false
 
  const encodePattern = new Map<string, number>()
  const encodeWord = new Map<string, number>()
 
  for (let i = 0; i < words.length; i++) {
    if (encodePattern.get(patterns[i]) !== encodeWord.get(words[i]))
      return false
 
    encodePattern.set(patterns[i], i)
    encodeWord.set(words[i], i)
  }
 
  return true
}