Although it is a simple problem, I did not think of a particularly elegant solution after reading the description of the problem. After reading the official solution, I felt that the bijective solution is really too easy to use

290. The rules of words are simple

Topic describes

``Given a pattern and a string STR, determine whether STR follows the same pattern. By following, we mean matching exactly. For example, there is a bidirectional connection between every letter in pattern and every non-empty word in the string STR. Example 1: Enter :pattern = "abba", STR = "dog cat cat dog" Output: true Example 2: Enter :pattern = "abba", STR = "dog cat cat fish" Output: False Example 3: Enter: pattern = "aAAA ", STR = "dog cat cat dog" Output: false Example 4: Enter: Pattern = "abba", STR = "dog dog dog" Output: false Description: You can assume that pattern contains only lowercase letters and that STR contains lowercase letters separated by a single space. Source: LeetCode Link: https://leetcode-cn.com/problems/word-pattern Copyright belongs to The Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.Copy the code``

Idea 1

1. Define two map
2. The key-value pairs on both maps are interchanged, thus confirming the correspondence
``````var wordPattern = function (pattern, s) {
let words = s.split(' ')
if(pattern.length ! = words.length)return false

let word2ch = new Map(a)let ch2word = new Map(a)// debugger
for(let [i, word] of words.entries()) {

// There is a word debugger for pattern
const ch = pattern[i]

/ / && priority is higher than | |
if(word2ch.get(ch) && word2ch.get(ch) ! = word || ch2word.has(word) && ch2word.get(word) ! == ch) {return false
}
word2ch.set(ch, word)
ch2word.set(word, ch)
}
return true
};
Copy the code``````

Idea 1 is de-processing by combining the values of the two

``````var wordPattern = function (pattern, s) {
const words = s.split("");
if(pattern.length ! == words.length)return false;
let h = [];
for (let i = 0; i < words.length; i++) {
h[i] = pattern[i] + "," + words[i];
}
const len = new Set(h).size;
return len == new Set(pattern).size && len == new Set(words).size;
}
Copy the code``````

Idea 2: Use the sum of the index values of the respective word elements in a string or array

``````var wordPattern = function (pattern, s) {
const words = s.split("");
if(pattern.length ! == words.length)return false;
// These two values cannot be assigned to 0; the lower indexOf will appear -1
let leftSum = ' ', rightSum = ' ', l = -1
while (++l < words.length) {
leftSum += pattern.indexOf(pattern[l])
rightSum += words.indexOf(words[l])
}
return leftSum === rightSum
}
Copy the code``````

Here is a direct bitwise comparison similar to the previous answer

``````var wordPattern = function (pattern, s) {
const words = s.split("");
if(pattern.length ! == words.length)return false;
for (let i = 0; i < words.length; i++) {
if(words.indexOf(words[i]) ! = pattern.indexOf(pattern[i]))return false
}
return true
}
Copy the code``````

Spelling similar words can sometimes teach yourself to be a good person ðŸ˜„