# LeetCode: Word Pattern Solution

2 hash tables

## Approach

One hash table to map pattern to word

One hash table to check if word is already mapped by a pattern

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}var wordPattern = function (pattern, s) {2  pattern = pattern.split("")3  s = s.split(" ")4
5  if (pattern.length !== s.length) return false6
7  const patternWordMap = new Map()8  const wordMapped = new Set()9
10  for (let i = 0; i < pattern.length; i++) {11    if (!patternWordMap.has(pattern[i]) && !wordMapped.has(s[i])) {12      patternWordMap.set(pattern[i], s[i])13      wordMapped.add(s[i])14      continue15    }16
17    if (patternWordMap.get(pattern[i]) !== s[i]) return false18  }19
20  return true21}```

Original problem

## Similar problems

Isomorphic Strings

Word Pattern II

leetcode

hash table

string

## Next Post

LeetCode: Power of Four

Oct 5, 2022

School math

## Previous Post

LeetCode: Symmetric Tree

Oct 2, 2022

Recursion

Search Posts