1. 程式人生 > >leetcode【890】 Find and Replace Pattern(來來來,你教我怎麼不用靜態變數做這道題?)

leetcode【890】 Find and Replace Pattern(來來來,你教我怎麼不用靜態變數做這道題?)

寫在最前面:

我真的是無語,本地和leetcode執行程式碼都過了,就是提交過不了,來來來,你教我怎麼不用靜態變數做這道題???

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.

Example 1:

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.

Note:

  • 1 <= words.length <= 50
  • 1 <= pattern.length = words[i].length <= 20

我的思路就是,每個單詞和模式建立一個字典的鍵值對,去掉值相同的鍵,然後匹配每個字元。

class Solution:
    def findAndReplacePattern(self, words, pattern):
        """
        :type words: List[str]
        :type pattern: str
        :rtype: List[str]
        """
        result = []
        fix = {}
        for word in words:
            for i in range(len(pattern)):
                fix[pattern[i]] = word[i]
            func = lambda z: dict([(x, y) for y, x in z.items()])
            fix = func(func(fix))
            if all(word[i] == fix.get(pattern[i]) for i in range(len(fix))):
                result.append(word)
                fix = {}
            else:
                fix = {}
        return result


Solution().findAndReplacePattern(["abc","deq","mee","aqq","dkd","ccc"],"abb")

相關推薦

leetcode890 Find and Replace Pattern怎麼不用靜態變數

寫在最前面: 我真的是無語,本地和leetcode執行程式碼都過了,就是提交過不了,來來來,你教我怎麼不用靜態變數做這道題??? You have a list of words and a pattern, and you want to know which

890. Find and Replace Patternpython+cpp

題目: 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 t

LeetCode 205. Isomorphic Strings; 290. Word Pattern; 890. Find and Replace Pattern

string lse cto return 建立 flag 一次 else leetcode 這幾道題都是pattern的題目, Isomorphic Strings 和 Word Pattern 是完全一樣的問題,Find and Replace Pattern 本質也一

LeetCode - 890. Find and Replace Pattern

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 p

LeetCode: 890. Find and Replace Pattern

題目:890. Find and Replace Pattern(https://leetcode.com/problems/find-and-replace-pattern/description/) 解法: 將元素的出現的位置放入陣列中,當元素第一次出現時,他在陣列中的值是預設

JavaScript刷LeetCode -- 890. Find and Replace Pattern

一、題目 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 ther

890. Find and Replace Pattern

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 ther

LeetCode Find and Replace Pattern 查詢和替換模式

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 ther

LC 890. Find and Replace Pattern

-s sub HERE != public tor order vector tin You have a list of words and a pattern, and you want to know which words in words matches the

leetcode162Find Peak Element

寫在最前面:這可能是我做過的最簡單的中等難度的題了   leetcode【162】Find Peak Element   A peak element is an element that is greater than its neighbors. Gi

HDOJfind your present (2)思路

利用 自己 names namespace pro ble get nbsp tdi find your present (2) 題意:找有唯一一個出現奇數次的數 思路:利用位異或。一個數異或自己本身是0; #include<stdio.h> int

CF613DKingdom and its Cities虛樹動態規劃

-c www. AI gis IE long long als space gist 【CF613D】Kingdom and its Cities(虛樹,動態規劃) 題面 洛谷 CF 翻譯洛谷上有啦 題解 每次構建虛樹,首先特判無解,也就是關鍵點中存在父子關系。 考慮\(d

HDOJ6222Heron and His TriangleJava二分遞推

ret 直接 for out ext n-1 multipl system angle 題意:讓你找這樣的一個三角形,三條邊為t,t-1,t+1,並且面積為整數,最後滿足t大於等於n。 n<=1e30 思路:直接推式子不會,打表找規律 f(n)=4*f(n-1)-f(

CF1016DVasya And The Matrix構造

題意: 思路:構造方式見程式碼…… 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 typedef long long

AtCoder2376Black and White Tree博弈

題意 A和B輪流給樹上的結點染色,A每次選擇沒染過的點染成白色,B每次選擇沒染過的點染成黑色,最後若所有白色都與黑色相鄰,則B勝,否則A勝。雙方以最優策略,求A勝還是B勝。 題解 A首先選擇葉子結點的父

MATLAB find 函式的使用線性索引

find 查詢非零元素的索引和值 Syntax k = find(X) k = find(X,n) k = find(X,n,direction) [row,col] = find(___) [row,col,v] = find(___) Descript

洛谷P3380 模板二逼平衡樹樹套樹樹狀數組線段樹

bre 就是 uniq nlog lin tdi 數組 比較 也有 洛谷題目傳送門 emm。。。題目名寫了個平衡樹,但是這道題的理論復雜度最優解應該還是樹狀數組套值域線段樹吧。 就像dynamic ranking那樣(蒟蒻的Sol,放一個link騙訪問量233) 所有的值(

BZOJ1047[HAOI2007]理想的正方形單調隊列動態規劃

geo cpp n) ace zoj != efi problem tchar 【BZOJ1047】[HAOI2007]理想的正方形(單調隊列,動態規劃) 題面 BZOJ 洛谷 題解 直接一個單調隊列維護一下沒給點和它前面的\(n\)個位置的最大值,再用一次單調隊列維護連續

BZOJ1998[HNOI2010]物品調度並查集模擬

turn show else int 維護 sin size problem pro 【BZOJ1998】[HNOI2010]物品調度(並查集,模擬) 題面 BZOJ,為啥這題都是權限題啊? 洛谷 題解 先不管\(0\)位置是個空,把它也看成一個箱子。那麽最終的答案顯然和置

BZOJ2229[ZJOI2011]最小割網絡流最小割樹

geo com ext pop != names pro truct str 【BZOJ2229】[ZJOI2011]最小割(網絡流,最小割樹) 題面 BZOJ 洛谷 題解 戳這裏 那麽實現過程就是任選兩點跑最小割更新答案,然後把點集劃分為和\(S\)聯通以及與\(T\)聯