Nav: << previous: 289.生命游戏 | next: 291.单词规律 II >>
Description
tab: English
<p>Given a <code>pattern</code> and a string <code>s</code>, find if <code>s</code> follows the same pattern.</p>
<p>Here <b>follow</b> means a full match, such that there is a bijection between a letter in <code>pattern</code> and a <b>non-empty</b> word in <code>s</code>. Specifically:</p>
<ul>
<li>Each letter in <code>pattern</code> maps to <strong>exactly</strong> one unique word in <code>s</code>.</li>
<li>Each unique word in <code>s</code> maps to <strong>exactly</strong> one letter in <code>pattern</code>.</li>
<li>No two letters map to the same word, and no two words map to the same letter.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">pattern = "abba", s = "dog cat cat dog"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
<p><strong>Explanation:</strong></p>
<p>The bijection can be established as:</p>
<ul>
<li><code>'a'</code> maps to <code>"dog"</code>.</li>
<li><code>'b'</code> maps to <code>"cat"</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">pattern = "abba", s = "dog cat cat fish"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">pattern = "aaaa", s = "dog cat cat dog"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= pattern.length <= 300</code></li>
<li><code>pattern</code> contains only lower-case English letters.</li>
<li><code>1 <= s.length <= 3000</code></li>
<li><code>s</code> contains only lowercase English letters and spaces <code>' '</code>.</li>
<li><code>s</code> <strong>does not contain</strong> any leading or trailing spaces.</li>
<li>All the words in <code>s</code> are separated by a <strong>single space</strong>.</li>
</ul>
---
[submissions](https://leetcode.com/problems/word-pattern/submissions/) | [solutions](https://leetcode.com/problems/word-pattern/solutions/)
tab: 中文
<p>给定一种规律 <code>pattern</code> 和一个字符串 <code>s</code> ,判断 <code>s</code> 是否遵循相同的规律。</p>
<p>这里的 <strong>遵循 </strong>指完全匹配,例如, <code>pattern</code> 里的每个字母和字符串 <code>s</code><strong> </strong>中的每个非空单词之间存在着双向连接的对应规律。具体来说:</p>
<ul>
<li><code>pattern</code> 中的每个字母都 <strong>恰好</strong> 映射到 <code>s</code> 中的一个唯一单词。</li>
<li><code>s</code> 中的每个唯一单词都 <strong>恰好</strong> 映射到 <code>pattern</code> 中的一个字母。</li>
<li>没有两个字母映射到同一个单词,也没有两个单词映射到同一个字母。</li>
</ul>
<p> </p>
<p><strong class="example">示例1:</strong></p>
<pre>
<strong>输入:</strong> pattern = <code>"abba"</code>, s = <code>"dog cat cat dog"</code>
<strong>输出:</strong> true</pre>
<p><strong class="example">示例 2:</strong></p>
<pre>
<strong>输入:</strong>pattern = <code>"abba"</code>, s = <code>"dog cat cat fish"</code>
<strong>输出:</strong> false</pre>
<p><strong class="example">示例 3:</strong></p>
<pre>
<strong>输入:</strong> pattern = <code>"aaaa"</code>, s = <code>"dog cat cat dog"</code>
<strong>输出:</strong> false</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= pattern.length <= 300</code></li>
<li><code>pattern</code> 只包含小写英文字母</li>
<li><code>1 <= s.length <= 3000</code></li>
<li><code>s</code> 只包含小写英文字母和 <code>' '</code></li>
<li><code>s</code> <strong>不包含</strong> 任何前导或尾随对空格</li>
<li><code>s</code> 中每个单词都被 <strong>单个空格 </strong>分隔</li>
</ul>
---
[提交记录](https://leetcode.cn/problems/word-pattern/submissions/) | [题解](https://leetcode.cn/problems/word-pattern/solution/)
Solutions & Notes
properties:
note.updated:
displayName: Last Updated
note.relative_links:
displayName: Related Links
note.desc:
displayName: Description
note.grade:
displayName: Rating
note.program_language:
displayName: Language
note.time_complexity:
displayName: TC
note.space_complexity:
displayName: SC
views:
- type: table
name: Solutions & Notes
filters:
and:
- file.hasLink(this.file)
- file.tags.containsAny("leetcode/solution", "leetcode/note")
order:
- file.name
- desc
- program_language
- time_complexity
- space_complexity
- grade
- relative_links
- updated
sort:
- property: grade
direction: ASC
- property: time_complexity
direction: ASC
- property: program_language
direction: ASC
columnSize:
file.name: 104
note.space_complexity: 65
note.grade: 126
Similar Problems
properties:
note.lcTopics:
displayName: Topics
note.lcAcRate:
displayName: AC Rate
note.favorites:
displayName: Favorites
note.grade:
displayName: Rating
note.translatedTitle:
displayName: Title (CN)
note.lcDifficulty:
displayName: Difficulty
views:
- type: table
name: Similar Problems
filters:
and:
- file.hasLink(this.file)
- similarQuestions.contains(this.file)
order:
- file.name
- translatedTitle
- lcTopics
- lcDifficulty
- lcAcRate
- grade
- favorites
sort:
- property: file.name
direction: ASC
- property: lcTopics
direction: DESC
columnSize:
note.translatedTitle: 240
note.lcTopics: 347
note.lcAcRate: 75
note.grade: 122