{"id":8520,"date":"2021-08-08T16:01:45","date_gmt":"2021-08-08T23:01:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8520"},"modified":"2021-08-08T16:03:54","modified_gmt":"2021-08-08T23:03:54","slug":"leetcode-1880-check-if-word-equals-summation-of-two-words","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1880-check-if-word-equals-summation-of-two-words\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1880. Check if Word Equals Summation of Two Words"},"content":{"rendered":"\n<p>The&nbsp;<strong>letter value<\/strong>&nbsp;of a letter is its position in the alphabet&nbsp;<strong>starting from 0<\/strong>&nbsp;(i.e.&nbsp;<code>'a' -&gt; 0<\/code>,&nbsp;<code>'b' -&gt; 1<\/code>,&nbsp;<code>'c' -&gt; 2<\/code>, etc.).<\/p>\n\n\n\n<p>The&nbsp;<strong>numerical value<\/strong>&nbsp;of some string of lowercase English letters&nbsp;<code>s<\/code>&nbsp;is the&nbsp;<strong>concatenation<\/strong>&nbsp;of the&nbsp;<strong>letter values<\/strong>&nbsp;of each letter in&nbsp;<code>s<\/code>, which is then&nbsp;<strong>converted<\/strong>&nbsp;into an integer.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, if&nbsp;<code>s = \"acb\"<\/code>, we concatenate each letter&#8217;s letter value, resulting in&nbsp;<code>\"021\"<\/code>. After converting it, we get&nbsp;<code>21<\/code>.<\/li><\/ul>\n\n\n\n<p>You are given three strings&nbsp;<code>firstWord<\/code>,&nbsp;<code>secondWord<\/code>, and&nbsp;<code>targetWord<\/code>, each consisting of lowercase English letters&nbsp;<code>'a'<\/code>&nbsp;through&nbsp;<code>'j'<\/code>&nbsp;<strong>inclusive<\/strong>.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code>&nbsp;<em>if the&nbsp;<strong>summation<\/strong>&nbsp;of the&nbsp;<strong>numerical values<\/strong>&nbsp;of&nbsp;<\/em><code>firstWord<\/code><em>&nbsp;and&nbsp;<\/em><code>secondWord<\/code><em>&nbsp;equals the&nbsp;<strong>numerical value<\/strong>&nbsp;of&nbsp;<\/em><code>targetWord<\/code><em>, or&nbsp;<\/em><code>false<\/code><em>&nbsp;otherwise.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> firstWord = \"acb\", secondWord = \"cba\", targetWord = \"cdb\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong>\nThe numerical value of firstWord is \"acb\" -&gt; \"021\" -&gt; 21.\nThe numerical value of secondWord is \"cba\" -&gt; \"210\" -&gt; 210.\nThe numerical value of targetWord is \"cdb\" -&gt; \"231\" -&gt; 231.\nWe return true because 21 + 210 == 231.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> firstWord = \"aaa\", secondWord = \"a\", targetWord = \"aab\"\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> \nThe numerical value of firstWord is \"aaa\" -&gt; \"000\" -&gt; 0.\nThe numerical value of secondWord is \"a\" -&gt; \"0\" -&gt; 0.\nThe numerical value of targetWord is \"aab\" -&gt; \"001\" -&gt; 1.\nWe return false because 0 + 0 != 1.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> firstWord = \"aaa\", secondWord = \"a\", targetWord = \"aaaa\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> \nThe numerical value of firstWord is \"aaa\" -&gt; \"000\" -&gt; 0.\nThe numerical value of secondWord is \"a\" -&gt; \"0\" -&gt; 0.\nThe numerical value of targetWord is \"aaaa\" -&gt; \"0000\" -&gt; 0.\nWe return true because 0 + 0 == 0.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= firstWord.length,&nbsp;secondWord.length,&nbsp;targetWord.length &lt;= 8<\/code><\/li><li><code>firstWord<\/code>,&nbsp;<code>secondWord<\/code>, and&nbsp;<code>targetWord<\/code>&nbsp;consist of lowercase English letters from&nbsp;<code>'a'<\/code>&nbsp;to&nbsp;<code>'j'<\/code>&nbsp;<strong>inclusive<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Tips: Write a reusable function to compute the score of a word.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool isSumEqual(string firstWord, string secondWord, string targetWord) {\n    auto score = [](string_view s) {\n      int ans = 0;\n      for (char c : s)\n        ans = ans * 10 + (c - 'a');\n      return ans;\n    };\n    return score(firstWord) + score(secondWord) == score(targetWord);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The&nbsp;letter value&nbsp;of a letter is its position in the alphabet&nbsp;starting from 0&nbsp;(i.e.&nbsp;&#8216;a&#8217; -&gt; 0,&nbsp;&#8216;b&#8217; -&gt; 1,&nbsp;&#8216;c&#8217; -&gt; 2, etc.). The&nbsp;numerical value&nbsp;of some string of lowercase&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[93,222,4],"class_list":["post-8520","post","type-post","status-publish","format-standard","hentry","category-string","tag-conversion","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8520"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8520\/revisions"}],"predecessor-version":[{"id":8522,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8520\/revisions\/8522"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}