class Solution {
public:
bool wordPattern(string pattern, string s) {
}
};
290. 单词规律
给定一种规律 pattern
和一个字符串 s
,判断 s
是否遵循相同的规律。
这里的 遵循 指完全匹配,例如, pattern
里的每个字母和字符串 s
中的每个非空单词之间存在着双向连接的对应规律。
示例1:
输入: pattern ="abba"
, s ="dog cat cat dog"
输出: true
示例 2:
输入:pattern ="abba"
, s ="dog cat cat fish"
输出: false
示例 3:
输入: pattern ="aaaa"
, s ="dog cat cat dog"
输出: false
提示:
1 <= pattern.length <= 300
pattern
只包含小写英文字母1 <= s.length <= 3000
s
只包含小写英文字母和 ' '
s
不包含 任何前导或尾随对空格s
中每个单词都被 单个空格 分隔原站题解
python3 解法, 执行用时: 44 ms, 内存消耗: 15.9 MB, 提交时间: 2023-09-27 15:25:43
class Solution: def wordPattern(self, pattern: str, s: str) -> bool: word2ch = dict() ch2word = dict() words = s.split() if len(pattern) != len(words): return False for ch, word in zip(pattern, words): if (word in word2ch and word2ch[word] != ch) or (ch in ch2word and ch2word[ch] != word): return False word2ch[word] = ch ch2word[ch] = word return True
javascript 解法, 执行用时: 60 ms, 内存消耗: 40.8 MB, 提交时间: 2023-09-27 15:25:23
/** * @param {string} pattern * @param {string} s * @return {boolean} */ var wordPattern = function(pattern, s) { const word2ch = new Map(); const ch2word = new Map(); const words = s.split(' '); if (pattern.length !== words.length) { return false; } for (const [i, word] of words.entries()) { const ch = pattern[i]; if (word2ch.has(word) && word2ch.get(word) != ch || ch2word.has(ch) && ch2word.get(ch) !== word) { return false; } word2ch.set(word, ch); ch2word.set(ch, word); } return true; };
java 解法, 执行用时: 0 ms, 内存消耗: 39.6 MB, 提交时间: 2023-09-27 15:25:11
class Solution { public boolean wordPattern(String pattern, String str) { Map<String, Character> str2ch = new HashMap<String, Character>(); Map<Character, String> ch2str = new HashMap<Character, String>(); int m = str.length(); int i = 0; for (int p = 0; p < pattern.length(); ++p) { char ch = pattern.charAt(p); if (i >= m) { return false; } int j = i; while (j < m && str.charAt(j) != ' ') { j++; } String tmp = str.substring(i, j); if (str2ch.containsKey(tmp) && str2ch.get(tmp) != ch) { return false; } if (ch2str.containsKey(ch) && !tmp.equals(ch2str.get(ch))) { return false; } str2ch.put(tmp, ch); ch2str.put(ch, tmp); i = j + 1; } return i >= m; } }
cpp 解法, 执行用时: 0 ms, 内存消耗: 6.9 MB, 提交时间: 2023-09-27 15:24:55
class Solution { public: bool wordPattern(string pattern, string str) { unordered_map<string, char> str2ch; unordered_map<char, string> ch2str; int m = str.length(); int i = 0; for (auto ch : pattern) { if (i >= m) { return false; } int j = i; while (j < m && str[j] != ' ') j++; const string &tmp = str.substr(i, j - i); if (str2ch.count(tmp) && str2ch[tmp] != ch) { return false; } if (ch2str.count(ch) && ch2str[ch] != tmp) { return false; } str2ch[tmp] = ch; ch2str[ch] = tmp; i = j + 1; } return i >= m; } };
golang 解法, 执行用时: 0 ms, 内存消耗: 2 MB, 提交时间: 2021-08-05 17:19:48
func wordPattern(pattern string, s string) bool { word2ch := map[string]byte{} ch2word := map[byte]string{} words := strings.Split(s, " ") if len(pattern) != len(words) { return false } for i, word := range words { ch := pattern[i] // && 优先级 高于 || if word2ch[word] > 0 && word2ch[word] != ch || ch2word[ch] != "" && ch2word[ch] != word { return false } word2ch[word] = ch ch2word[ch] = word } return true }