首页 > 其他分享 >LeetCode: 290. Word Pattern

LeetCode: 290. Word Pattern

时间:2022-12-05 18:36:21浏览次数:42  
标签:Word words Pattern dog wordsMap str pattern cat LeetCode


LeetCode: 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​​.

Example 1:

Input: pattern = "abba", str = "dog cat cat dog"
Output: true

Example 2:

Input:pattern = "abba", str = "dog cat cat fish"
Output: false

Example 3:

Input: pattern = "aaaa", str = "dog cat cat dog"
Output: false

Example 4:

Input: pattern = "abba", str = "dog dog dog dog"
Output: false

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

解题思路 —— 哈希

由于 ​​pattern​​​ 是小写字母组成,因此可以用 ​​pattern[i] - 'a'​​​ 来映射 ​​pattern[i]​​​。
因此就可以用 ​​​var wordsMap [26]string​​​ 数组来存放 ​​pattern​​​ 和 ​​str​​ 中的单词的映射关系。

AC 代码

func wordPattern(pattern string, str string) bool {
words := strings.Split(str, " ")
var wordsMap [26]string

if len(pattern) != len(words) {
return false
}

for i := 0; i < len(words); i++{
if wordsMap[pattern[i]-'a'] == "" {
for j := 0; j < len(wordsMap); j++{
if wordsMap[j] == words[i] {
return false
}
}
wordsMap[pattern[i]-'a'] = words[i]
} else if wordsMap[pattern[i]-'a'] != words[i] {
return false
}
}

return true
}


标签:Word,words,Pattern,dog,wordsMap,str,pattern,cat,LeetCode
From: https://blog.51cto.com/u_15903085/5913218

相关文章

  • LeetCode: 310. Minimum Height Trees
    LeetCode:310.MinimumHeightTrees题目描述Foranundirectedgraphwithtreecharacteristics,wecanchooseanynodeastheroot.Theresultgraphisthenaro......
  • LeetCode: 312. Burst Balloons
    LeetCode:312.BurstBalloons题目描述Givennballoons,indexedfrom0ton-1.Eachballoonispaintedwithanumberonitrepresentedbyarraynums.Youareas......
  • LeetCode: 328. Odd Even Linked List
    LeetCode:328.OddEvenLinkedList题目描述Givenasinglylinkedlist,groupalloddnodestogetherfollowedbytheevennodes.Pleasenoteherewearetalking......
  • LeetCode: 313. Super Ugly Number
    LeetCode:313.SuperUglyNumberWriteaprogramtofindthenthsuperuglynumber.Superuglynumbersarepositivenumberswhoseallprimefactorsareinthegi......
  • LeetCode: 315. Count of Smaller Numbers After Self
    LeetCode:315.CountofSmallerNumbersAfterSelf题目描述Youaregivenanintegerarraynumsandyouhavetoreturnanewcountsarray.Thecountsarrayhast......
  • LeetCode: 322. Coin Change
    LeetCode:322.CoinChange题目描述Youaregivencoinsofdifferentdenominationsandatotalamountofmoneyamount.Writeafunctiontocomputethefewestnum......
  • LeetCode:295. Find Median from Data Stream
    LeetCode:295.FindMedianfromDataStream题目描述Medianisthemiddlevalueinanorderedintegerlist.Ifthesizeofthelistiseven,thereisnomiddleval......
  • LeetCode:282. Expression Add Operators
    LeetCode:282.ExpressionAddOperators题目描述Givenastringthatcontainsonlydigits0-9andatargetvalue,returnallpossibilitiestoaddbinaryoperators......
  • LeetCode:224.Basic Calculator
    LeetCode:224.BasicCalculator题目描述Implementabasiccalculatortoevaluateasimpleexpressionstring.Theexpressionstringmaycontainopen​​(​​​and......
  • LeetCode: 221. Maximal Square
    LeetCode:221.MaximalSquare题目描述Givena2Dbinarymatrixfilledwith​​0​​​'sand​​1​​​'s,findthelargestsquarecontainingonly​​1​​'s......