天天看點

【LeetCode】290. Word Pattern 解題報告(Python)

作者: 負雪明燭

id: fuxuemingzhu

個人部落格: http://fuxuemingzhu.cn/

題目描述

題目大意

解題方法

日期

題目位址:https://leetcode.com/problems/word-pattern/#/description

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:

Notes:

You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.

看模式和空格分割的字元串模式能不能構成一一映射。

這個題第一感覺就是使用HashMap,看到Tags也是HashMap我就放心了。想法基本就是把字元和單詞對應起來,看單詞和字元能不能對應上,這樣就知道模式是否比對了。做的時候出現了一個小問題,就是<code>pattern = "abba", str = "dog dog dog dog"</code>這種,要判斷是不是已經放進了value,之前不知道怎麼辦,今天學到了HashMap有個containsValue()方法,可以判斷是否已經放進了value。

二刷,使用Python解法,這個題和205. Isomorphic Strings一模一樣,是以很快就寫出來這個一一映射的代碼:

2017 年 5 月 19 日

2018 年 11 月 24 日 —— 周六快樂