-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yoozo
committed
Dec 16, 2020
1 parent
1b86abd
commit 956f886
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 290. 单词规律. | ||
// https://leetcode-cn.com/problems/word-pattern/ | ||
|
||
/** | ||
* @param {string} pattern | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
var wordPattern = function (pattern, s) { | ||
const pMap = new Map() | ||
for (let i = 0; i < pattern.length; i++) { | ||
const c = pattern[i] | ||
if (pMap.has(c)) { | ||
pMap.get(c).push(i) | ||
} else { | ||
pMap.set(c, [i]) | ||
} | ||
} | ||
const sArray = s.split(' ') | ||
if (pattern.length !== sArray.length) { | ||
return false | ||
} | ||
const word = [] | ||
for (const item of pMap) { | ||
if (!word.includes(sArray[item[1][0]])) { | ||
word.push(sArray[item[1][0]]) | ||
} else { | ||
return false | ||
} | ||
for (let i = 1; i < item[1].length; i++) { | ||
if (sArray[item[1][0]] !== sArray[item[1][i]]) { | ||
return false | ||
} | ||
} | ||
} | ||
return true | ||
}; | ||
|
||
|
||
console.log(wordPattern("abba", "dog dd dd dog")) |