Nav: << previous: 90.子集 II | next: 92.反转链表 II >>


Description

tab: English
 
<p>You have intercepted a secret message encoded as a string of numbers. The message is <strong>decoded</strong> via the following mapping:</p>
 
<p><code>&quot;1&quot; -&gt; &#39;A&#39;<br />
&quot;2&quot; -&gt; &#39;B&#39;<br />
...<br />
&quot;25&quot; -&gt; &#39;Y&#39;<br />
&quot;26&quot; -&gt; &#39;Z&#39;</code></p>
 
<p>However, while decoding the message, you realize that there are many different ways you can decode the message because some codes are contained in other codes (<code>&quot;2&quot;</code> and <code>&quot;5&quot;</code> vs <code>&quot;25&quot;</code>).</p>
 
<p>For example, <code>&quot;11106&quot;</code> can be decoded into:</p>
 
<ul>
	<li><code>&quot;AAJF&quot;</code> with the grouping <code>(1, 1, 10, 6)</code></li>
	<li><code>&quot;KJF&quot;</code> with the grouping <code>(11, 10, 6)</code></li>
	<li>The grouping <code>(1, 11, 06)</code> is invalid because <code>&quot;06&quot;</code> is not a valid code (only <code>&quot;6&quot;</code> is valid).</li>
</ul>
 
<p>Note: there may be strings that are impossible to decode.<br />
<br />
Given a string s containing only digits, return the <strong>number of ways</strong> to <strong>decode</strong> it. If the entire string cannot be decoded in any valid way, return <code>0</code>.</p>
 
<p>The test cases are generated so that the answer fits in a <strong>32-bit</strong> integer.</p>
 
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;12&quot;</span></p>
 
<p><strong>Output:</strong> <span class="example-io">2</span></p>
 
<p><strong>Explanation:</strong></p>
 
<p>&quot;12&quot; could be decoded as &quot;AB&quot; (1 2) or &quot;L&quot; (12).</p>
</div>
 
<p><strong class="example">Example 2:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;226&quot;</span></p>
 
<p><strong>Output:</strong> <span class="example-io">3</span></p>
 
<p><strong>Explanation:</strong></p>
 
<p>&quot;226&quot; could be decoded as &quot;BZ&quot; (2 26), &quot;VF&quot; (22 6), or &quot;BBF&quot; (2 2 6).</p>
</div>
 
<p><strong class="example">Example 3:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;06&quot;</span></p>
 
<p><strong>Output:</strong> <span class="example-io">0</span></p>
 
<p><strong>Explanation:</strong></p>
 
<p>&quot;06&quot; cannot be mapped to &quot;F&quot; because of the leading zero (&quot;6&quot; is different from &quot;06&quot;). In this case, the string is not a valid encoding, so return 0.</p>
</div>
 
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
 
<ul>
	<li><code>1 &lt;= s.length &lt;= 100</code></li>
	<li><code>s</code> contains only digits and may contain leading zero(s).</li>
</ul>
 
 
 
---
 
[submissions](https://leetcode.com/problems/decode-ways/submissions/) | [solutions](https://leetcode.com/problems/decode-ways/solutions/)
 
 
tab: 中文
 
<p>一条包含字母&nbsp;<code>A-Z</code> 的消息通过以下映射进行了 <strong>编码</strong> :</p>
 
<p><code>"1" -&gt; 'A'<br />
"2" -&gt; 'B'<br />
...<br />
"25" -&gt; 'Y'<br />
"26" -&gt; 'Z'</code></p>
 
<p>然而,在&nbsp;<strong>解码</strong> 已编码的消息时,你意识到有许多不同的方式来解码,因为有些编码被包含在其它编码当中(<code>"2"</code> 和 <code>"5"</code> 与 <code>"25"</code>)。</p>
 
<p>例如,<code>"11106"</code> 可以映射为:</p>
 
<ul>
	<li><code>"AAJF"</code> ,将消息分组为 <code>(1, 1, 10, 6)</code></li>
	<li><code>"KJF"</code> ,将消息分组为 <code>(11, 10, 6)</code></li>
	<li>消息不能分组为&nbsp; <code>(1, 11, 06)</code> ,因为 <code>"06"</code>&nbsp;不是一个合法编码(只有 "6" 是合法的)。</li>
</ul>
 
<p>注意,可能存在无法解码的字符串。</p>
 
<p>给你一个只含数字的 <strong>非空 </strong>字符串 <code>s</code> ,请计算并返回 <strong>解码</strong> 方法的 <strong>总数</strong> 。如果没有合法的方式解码整个字符串,返回 <code>0</code>。</p>
 
<p>题目数据保证答案肯定是一个 <strong>32 位</strong> 的整数。</p>
 
<p>&nbsp;</p>
 
<p><strong>示例 1:</strong></p>
 
<pre>
<strong>输入:</strong>s = "12"
<strong>输出:</strong>2
<strong>解释:</strong>它可以解码为 "AB"(1 2)或者 "L"(12)。
</pre>
 
<p><strong>示例 2:</strong></p>
 
<pre>
<strong>输入:</strong>s = "226"
<strong>输出:</strong>3
<strong>解释:</strong>它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。
</pre>
 
<p><strong>示例 3:</strong></p>
 
<pre>
<strong>输入:</strong>s = "06"
<strong>输出:</strong>0
<strong>解释:</strong>"06" 无法映射到 "F" ,因为存在前导零("6" 和 "06" 并不等价)。
</pre>
 
<p>&nbsp;</p>
 
<p><strong>提示:</strong></p>
 
<ul>
	<li><code>1 &lt;= s.length &lt;= 100</code></li>
	<li><code>s</code> 只包含数字,并且可能包含前导零。</li>
</ul>
 
 
 
---
 
[提交记录](https://leetcode.cn/problems/decode-ways/submissions/) | [题解](https://leetcode.cn/problems/decode-ways/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