洞察纵观鸿蒙next版本,如何凭借FinClip加强小程序的跨平台管理,确保企业在数字化转型中的高效运营和数据安全?
555
2022-11-11
290. Word Pattern
Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
Examples: pattern = “abba”, str = “dog cat cat dog” should return true. pattern = “abba”, str = “dog cat cat fish” should return false. pattern = “aaaa”, str = “dog cat cat dog” should return false. pattern = “abba”, str = “dog dog dog dog” should return false. Notes: You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
class Solution { public boolean wordPattern(String pattern, String str) { String[] strs = str.split(" "); if(pattern.length() != strs.length) return false; Map
/** * @param {string} pattern * @param {string} str * @return {boolean} */var wordPattern = function(pattern, str) { const pArr = pattern.split('') const sArr = str.split(' ') const pLen = pArr.length const sLen = sArr.length if (pLen !== sLen) { return false } const mapP = new Map() const mapS = new Map() for (let i = 0; i < pLen; i++) { const pat = pArr[i] const s = sArr[i] if (!mapP.has(pat)) { mapP.set(pat, s) } else { if (mapP.get(pat) !== s) { return false } } if (!mapS.has(s)) { mapS.set(s, pat) } else { if (mapS.get(s) !== pat) { return false } } } return true};
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~