1880:检查某单词是否等于两单词之和

This commit is contained in:
huangge1199 2021-06-07 13:14:28 +08:00
parent 465dff768d
commit 120d34f170
2 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,86 @@
//字母的 字母值 取决于字母在字母表中的位置 0 开始 计数'a' -> 0'b' -> 1'c' -> 2以此类推
//
// 对某个由小写字母组成的字符串 s 而言 数值 就等于将 s 中每个字母的 字母值 按顺序 连接 转换 成对应整数
//
//
// 例如s = "acb" 依次连接每个字母的字母值可以得到 "021" 转换为整数得到 21
//
//
// 给你三个字符串 firstWordsecondWord targetWord 每个字符串都由从 'a' 'j' 'a' 'j'
//小写英文字母组成
//
// 如果 firstWord secondWord 数值之和 等于 targetWord 的数值返回 true 否则返回 false
//
//
//
// 示例 1
//
// 输入firstWord = "acb", secondWord = "cba", targetWord = "cdb"
//输出true
//解释
//firstWord 的数值为 "acb" -> "021" -> 21
//secondWord 的数值为 "cba" -> "210" -> 210
//targetWord 的数值为 "cdb" -> "231" -> 231
//由于 21 + 210 == 231 返回 true
//
//
// 示例 2
//
// 输入firstWord = "aaa", secondWord = "a", targetWord = "aab"
//输出false
//解释
//firstWord 的数值为 "aaa" -> "000" -> 0
//secondWord 的数值为 "a" -> "0" -> 0
//targetWord 的数值为 "aab" -> "001" -> 1
//由于 0 + 0 != 1 返回 false
//
// 示例 3
//
// 输入firstWord = "aaa", secondWord = "a", targetWord = "aaaa"
//输出true
//解释
//firstWord 的数值为 "aaa" -> "000" -> 0
//secondWord 的数值为 "a" -> "0" -> 0
//targetWord 的数值为 "aaaa" -> "0000" -> 0
//由于 0 + 0 == 0 返回 true
//
//
//
//
// 提示
//
//
// 1 <= firstWord.length, secondWord.length, targetWord.length <= 8
// firstWordsecondWord targetWord 仅由从 'a' 'j' 'a' 'j' 的小写英文字母组成
//
// Related Topics 字符串
// 👍 4 👎 0
package leetcode.editor.cn;
//1880:检查某单词是否等于两单词之和
public class CheckIfWordEqualsSummationOfTwoWords{
public static void main(String[] args) {
//测试代码
Solution solution = new CheckIfWordEqualsSummationOfTwoWords().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
return trans(firstWord) + trans(secondWord) == trans(targetWord);
}
private int trans(String str) {
while (str.length() > 1 && str.startsWith("a")) {
str = str.substring(1);
}
StringBuilder numStr = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
numStr.append(str.charAt(i) - 'a');
}
return Integer.parseInt(numStr.toString());
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,55 @@
<p>字母的 <strong>字母值</strong> 取决于字母在字母表中的位置,<strong>从 0 开始</strong> 计数。即,<code>'a' -&gt; 0</code><code>'b' -&gt; 1</code><code>'c' -&gt; 2</code>,以此类推。</p>
<p>对某个由小写字母组成的字符串 <code>s</code> 而言,其 <strong>数值</strong> 就等于将 <code>s</code> 中每个字母的 <strong>字母值</strong> 按顺序 <strong>连接</strong><strong>转换</strong> 成对应整数。</p>
<ul>
<li>例如,<code>s = "acb"</code> ,依次连接每个字母的字母值可以得到 <code>"021"</code> ,转换为整数得到 <code>21</code></li>
</ul>
<p>给你三个字符串 <code>firstWord</code><code>secondWord</code><code>targetWord</code> ,每个字符串都由从 <code>'a'</code><code>'j'</code> <strong>含 </strong><code>'a'</code><code>'j'</code><strong> </strong>)的小写英文字母组成。</p>
<p>如果 <code>firstWord</code><em> </em><em> </em><code>secondWord</code><strong>数值之和</strong> 等于<em> </em><code>targetWord</code><em> </em>的数值,返回 <code>true</code> ;否则,返回<em> </em><code>false</code><em> </em></p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>firstWord = "acb", secondWord = "cba", targetWord = "cdb"
<strong>输出:</strong>true
<strong>解释:</strong>
firstWord 的数值为 "acb" -&gt; "021" -&gt; 21
secondWord 的数值为 "cba" -&gt; "210" -&gt; 210
targetWord 的数值为 "cdb" -&gt; "231" -&gt; 231
由于 21 + 210 == 231 ,返回 true
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>firstWord = "aaa", secondWord = "a", targetWord = "aab"
<strong>输出:</strong>false
<strong>解释:</strong>
firstWord 的数值为 "aaa" -&gt; "000" -&gt; 0
secondWord 的数值为 "a" -&gt; "0" -&gt; 0
targetWord 的数值为 "aab" -&gt; "001" -&gt; 1
由于 0 + 0 != 1 ,返回 false</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>firstWord = "aaa", secondWord = "a", targetWord = "aaaa"
<strong>输出:</strong>true
<strong>解释:</strong>
firstWord 的数值为 "aaa" -&gt; "000" -&gt; 0
secondWord 的数值为 "a" -&gt; "0" -&gt; 0
targetWord 的数值为 "aaaa" -&gt; "0000" -&gt; 0
由于 0 + 0 == 0 ,返回 true
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= firstWord.length, </code><code>secondWord.length, </code><code>targetWord.length &lt;= 8</code></li>
<li><code>firstWord</code><code>secondWord</code><code>targetWord</code> 仅由从 <code>'a'</code><code>'j'</code> <strong>含 </strong><code>'a'</code><code>'j'</code><strong> </strong>)的小写英文字母组成<strong></strong></li>
</ul>
<div><div>Related Topics</div><div><li>字符串</li></div></div>\n<div><li>👍 4</li><li>👎 0</li></div>