diff --git a/Algorithms/290. Word Pattern/290. Word Pattern.go b/Algorithms/290. Word Pattern/290. Word Pattern.go new file mode 100644 index 00000000..7ffcd1c8 --- /dev/null +++ b/Algorithms/290. Word Pattern/290. Word Pattern.go @@ -0,0 +1,31 @@ +package leetcode + +import "strings" + +func wordPattern(pattern string, str string) bool { + strList := strings.Split(str, " ") + patternByte := []byte(pattern) + if pattern == "" || len(patternByte) != len(strList) { + return false + } + + pMap := map[byte]string{} + sMap := map[string]byte{} + for index, b := range patternByte { + if _, ok := pMap[b]; !ok { + if _, ok = sMap[strList[index]]; !ok { + pMap[b] = strList[index] + sMap[strList[index]] = b + } else { + if sMap[strList[index]] != b { + return false + } + } + } else { + if pMap[b] != strList[index] { + return false + } + } + } + return true +} diff --git a/Algorithms/290. Word Pattern/290. Word Pattern_test.go b/Algorithms/290. Word Pattern/290. Word Pattern_test.go new file mode 100644 index 00000000..c9356501 --- /dev/null +++ b/Algorithms/290. Word Pattern/290. Word Pattern_test.go @@ -0,0 +1,58 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question290 struct { + para290 + ans290 +} + +// para 是参数 +// one 代表第一个参数 +type para290 struct { + one string + two string +} + +// ans 是答案 +// one 代表第一个答案 +type ans290 struct { + one bool +} + +func Test_Problem290(t *testing.T) { + + qs := []question290{ + + question290{ + para290{"abba", "dog cat cat dog"}, + ans290{true}, + }, + + question290{ + para290{"abba", "dog cat cat fish"}, + ans290{false}, + }, + + question290{ + para290{"aaaa", "dog cat cat dog"}, + ans290{false}, + }, + + question290{ + para290{"abba", "dog dog dog dog"}, + ans290{false}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 290------------------------\n") + + for _, q := range qs { + _, p := q.ans290, q.para290 + fmt.Printf("【input】:%v 【output】:%v\n", p, wordPattern(p.one, p.two)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/290. Word Pattern/README.md b/Algorithms/290. Word Pattern/README.md new file mode 100644 index 00000000..1406505c --- /dev/null +++ b/Algorithms/290. Word Pattern/README.md @@ -0,0 +1,49 @@ +# [290. Word Pattern](https://leetcode.com/problems/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. + +Example 1: + +```c +Input: pattern = "abba", str = "dog cat cat dog" +Output: true +``` + +Example 2: + +```c +Input:pattern = "abba", str = "dog cat cat fish" +Output: false +``` + +Example 3: + +```c +Input: pattern = "aaaa", str = "dog cat cat dog" +Output: false +``` + +Example 4: + +```c +Input: pattern = "abba", str = "dog dog dog dog" +Output: false +``` + +Note: + +You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space. + + +## 题目大意 + +给定一个模式串,判断字符串是否和给定的模式串,是一样的模式。 + +这道题用 2 个 map 即可。1 个 map 记录模式与字符串的匹配关系,另外一个 map 记录字符串和模式的匹配关系。为什么需要记录双向的关系呢?因为 Example 4 中,a 对应了 dog,这个时候 b 如果再对应 dog 是错误的,所以这里需要从 dog 查询它是否已经和某个模式匹配过了。所以需要双向的关系。 + + +