1. 題目
You have a list of words and a pattern, and you want to know which words in words matches the pattern.
A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.
(Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.)
Return a list of the words in words that match the given pattern.
You may return the answer in any order.
例子
Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb" Output: ["mee","aqq"] Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}. "ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation, since a and b map to the same letter.2. 我的解法
var findAndReplacePattern = function(words, pattern) { const norPatter = toNormal(pattern) return words.filter( v => toNormal(v) === norPatter) }; var toNormal = function(string) { let li = string.split("") let tem = [] let tem2 = [] let i = 0 li.forEach(v => { let index = tem2.indexOf(v) if(index===-1) { i++ tem.push(i) } else { tem.push(tem[index]) } tem2.push(v) }) return tem.join("") }
Runtime: 60 ms, faster than 99.18% of JavaScript online submissions for Find and Replace Pattern.
Memory Usage: 35.1 MB, less than 54.17% of JavaScript online submissions for Find and Replace Pattern.
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/103941.html
摘要:最簡(jiǎn)單的查找替換在中查找和替換非常簡(jiǎn)單,如果當(dāng)前對(duì)象是一個(gè)字符串時(shí),你可以使用該類型提供的或者方法查找指定的字符,如果能找到則會(huì)返回字符第一次出現(xiàn)的索引,如果不存在則返回。下面是正則查找的簡(jiǎn)單示例。 最簡(jiǎn)單的查找替換 在Python中查找和替換非常簡(jiǎn)單,如果當(dāng)前對(duì)象是一個(gè)字符串str時(shí),你可以使用該類型提供的find()或者index()方法查找指定的字符,如果能找到則會(huì)返回字符第一次...
摘要:難度題目給出一個(gè)字符串和一個(gè)要求我們給出這個(gè)字符串是否匹配這個(gè)其中通配符跟我們平常見(jiàn)到的一樣是和代表任意單個(gè)字符代表一個(gè)或多個(gè)字符這個(gè)題跟簡(jiǎn)單正則匹配比較類似可以跟這里面第二個(gè)解法一樣采取類似的動(dòng)態(tài)規(guī)劃解法在里取中間某個(gè)確定的字符串序列將字 Implement wildcard pattern matching with support for ? and *. ? Matches ...
摘要:是決定正則表達(dá)式匹配規(guī)則的主要部分。二分隔符分隔符的選擇當(dāng)使用函數(shù)的時(shí)候,正則表達(dá)式必須由分隔符閉合包裹。果分隔符經(jīng)常在正則表達(dá)式內(nèi)出現(xiàn),最好使用其他分隔符來(lái)提高可讀性。需要將一個(gè)字符串放入正則表達(dá)式中使用時(shí),可以用函數(shù)對(duì)其進(jìn)行轉(zhuǎn)義。 一、簡(jiǎn)介 1. 什么是正則表達(dá)式 正則表達(dá)式(Regular Expression)就是用某種模式去匹配一類字符串的一種公式。正則表達(dá)式使用單個(gè)字符串來(lái)...
閱讀 1971·2019-08-30 15:54
閱讀 3596·2019-08-29 13:07
閱讀 3124·2019-08-29 12:39
閱讀 1789·2019-08-26 12:13
閱讀 1547·2019-08-23 18:31
閱讀 2159·2019-08-23 18:05
閱讀 1844·2019-08-23 18:00
閱讀 1043·2019-08-23 17:15