{"id":10914,"date":"2019-02-04T00:51:23","date_gmt":"2019-02-04T00:51:23","guid":{"rendered":"http:\/\/www.softwareeverydayblog.com\/?p=10914"},"modified":"2019-02-04T01:10:25","modified_gmt":"2019-02-04T01:10:25","slug":"integer-to-english-words","status":"publish","type":"post","link":"https:\/\/www.softwareeverydayblog.com\/?p=10914","title":{"rendered":"Integer to English Words"},"content":{"rendered":"<p>Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 2<sup>31<\/sup> &#8211; 1.<\/p>\n<p>[<a href=\"https:\/\/leetcode.com\/problems\/integer-to-english-words\/description\/\" rel=\"noopener\" target=\"_blank\">Problem Link<\/a>]<\/p>\n<ol>\n<li>Create a list of numbers for which you have to know the text. For example, there is no way you could come up with text for 1-9, 10-20, 30, 40&#8230; without knowing them!<\/li>\n<li>Create a subroutine that solves this problem for 3 digit numbers (up-to 999). If you find the number in the above map return string, else build it. For eg. 123\n<pre lang=\"text\">\r\n1            2 0          3\r\n- - -      | - - -      | - - -\r\n^ Hundred    ^ Twenty     ^ There    \r\n<\/pre>\n<\/li>\n<li>Use the above routine to convert digits in batches of 3. Depending on the batch, add Thousand, Million and\/or Billion to the converted batch of 3 digits. E.g. to convert 2,190,876,001\n<pre lang=\"text\">\r\n0 0 2          1 9 0        8 7 6         0 0 1\r\n- - -        | - - -      | - - -       | - - -\r\n^ Billion    ^ Million    ^ Thousand    \r\n<\/pre>\n<\/li>\n<\/ol>\n<p><strong>Gotcha&#8217;s<\/strong><\/p>\n<ul>\n<li>If num is 0 output &#8220;Zero&#8221;. This is the only case you will use zero.<\/li>\n<li>If any of the 3 digits in the batches are 0, output nothing! E.g. 1000123, make sure you don&#8217;t output an extra Thousand anywhere, meaning something like &#8220;One Million Thousand One Hundred Twenty Three&#8221;.<\/li>\n<\/ul>\n<pre lang=\"java\">\r\nclass Solution {\r\n    \r\n    Map<Integer, String> num2wordsMap = new HashMap<Integer, String>() {{\r\n        put(1, \"One\");\r\n        put(2, \"Two\");\r\n        put(3, \"Three\");\r\n        put(4, \"Four\");\r\n        put(5, \"Five\");\r\n        put(6, \"Six\");\r\n        put(7, \"Seven\");\r\n        put(8, \"Eight\");\r\n        put(9, \"Nine\");\r\n        put(10, \"Ten\");\r\n        put(11, \"Eleven\");\r\n        put(12, \"Twelve\");\r\n        put(13, \"Thirteen\");\r\n        put(14, \"Fourteen\");\r\n        put(15, \"Fifteen\");\r\n        put(16, \"Sixteen\");\r\n        put(17, \"Seventeen\");\r\n        put(18, \"Eighteen\");\r\n        put(19, \"Nineteen\");\r\n        put(20, \"Twenty\");\r\n        put(30, \"Thirty\");\r\n        put(40, \"Forty\");\r\n        put(50, \"Fifty\");\r\n        put(60, \"Sixty\");\r\n        put(70, \"Seventy\");\r\n        put(80, \"Eighty\");\r\n        put(90, \"Ninety\");\r\n        put(100, \"One Hundred\");\r\n        put(200, \"Two Hundred\");\r\n        put(300, \"Three Hundred\");\r\n        put(400, \"Four Hundred\");\r\n        put(500, \"Five Hundred\");\r\n        put(600, \"Six Hundred\");\r\n        put(700, \"Seven Hundred\");\r\n        put(800, \"Eight Hundred\");\r\n        put(900, \"Nine Hundred\");\r\n    }};\r\n    \r\n    public String numberToWords(int num) {\r\n        if (num == 0)\r\n            return \"Zero\";\r\n        \r\n        String ret = \"\";\r\n        int index = 0;\r\n        while ( num > 0 ) {\r\n            int lastThree = 0;\r\n            lastThree += (num % 10);\r\n            num \/= 10;\r\n            lastThree += ((num % 10)*10);\r\n            num \/= 10;\r\n            lastThree += ((num % 10)*100);\r\n            num \/= 10;\r\n            \r\n            if (lastThree==0) {\r\n                index++;\r\n                continue;\r\n            }\r\n                        \r\n            String threeDigitRet = threeDigitNumberToWords(lastThree);\r\n            if ( index == 0 ) {\r\n                ret = threeDigitRet;\r\n            } else if ( index == 1 ) {\r\n                ret = threeDigitRet + \" Thousand \" + ret;\r\n            } else if ( index == 2 ) {\r\n                ret = threeDigitRet + \" Million \" + ret;\r\n            } else if ( index == 3 ) {\r\n                ret = threeDigitRet + \" Billion \" + ret;\r\n            }\r\n            \r\n            index++;\r\n        }\r\n        return ret.trim();\r\n    }\r\n    \r\n    \/**\r\n    * convert 3 numbers to text\r\n    *\/\r\n    public String threeDigitNumberToWords(int num) {\r\n        int units = num % 10;\r\n        int tens = (num\/10) % 10;\r\n        int hundreds = (num\/100) % 10;\r\n        int tensNum = tens*10 + units;\r\n        \r\n        String ret = \"\";\r\n        if (!numberToWordsMap(num).equals(\"\")) {\r\n            ret += numberToWordsMap(num);\r\n        } else {\r\n            ret += hundreds != 0 ? numberToWordsMap(hundreds) + \" Hundred \" : \"\";\r\n            if (!numberToWordsMap(tensNum).equals(\"\")) {\r\n                ret += numberToWordsMap(tensNum);\r\n            } else {\r\n                ret += (tens != 0 ? numberToWordsMap(tens*10) + \" \" : \"\");\r\n                ret += numberToWordsMap(units);\r\n            }\r\n        }\r\n\r\n        return ret;\r\n    }\r\n    \r\n    public String numberToWordsMap(int n) {\r\n        return num2wordsMap.getOrDefault(n, \"\");\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 &#8211; 1. [Problem Link] Create a list of numbers for which you have to know the text. For example, there is no way you could come up with text for 1-9, 10-20, 30, 40&#8230; without knowing them! [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10914","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/10914","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10914"}],"version-history":[{"count":2,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/10914\/revisions"}],"predecessor-version":[{"id":10917,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=\/wp\/v2\/posts\/10914\/revisions\/10917"}],"wp:attachment":[{"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwareeverydayblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}