{"id":440,"date":"2018-09-11T10:30:44","date_gmt":"2018-09-11T10:30:44","guid":{"rendered":"http:\/\/emacslisp.com\/?p=440"},"modified":"2018-09-11T10:30:53","modified_gmt":"2018-09-11T10:30:53","slug":"backtrace-leetcode-17-letter-combinations-of-a-phone-number","status":"publish","type":"post","link":"http:\/\/emacslisp.com\/?p=440","title":{"rendered":"Backtrace &#8211; leetcode 17 &#8211; Letter Combinations of a Phone Number"},"content":{"rendered":"<p>this is a very classic backtrace problem.<\/p>\n<p>Idea is simple, use &#8216;call stack&#8217; to store string &#8220;a&#8230;z&#8221;<\/p>\n<p>stack n &#8211; z<br \/>\n.<br \/>\n.<br \/>\nstack 1 &#8211; a<\/p>\n<p>if stack.length == targetLenth<br \/>\n   loop though stack to get char array to string.<\/p>\n<pre lang=\"java\" line=\"1\">\r\n\r\npackage com.dw.leetcode;\r\n\r\nimport java.util.*;\r\n\r\npublic class Letter_Combinations_of_a_Phone_Number_17 {\r\n\tstatic final char[][] number = { { 'a', 'b', 'c' }, { 'd', 'e', 'f' }, { 'g', 'h', 'i' }, { 'j', 'k', 'l' },\r\n\t\t\t{ 'm', 'n', 'o' }, { 'p', 'q', 'r', 's' }, { 't', 'u', 'v' }, { 'w', 'x', 'y', 'z' }\r\n\t};\r\n\t\r\n    public List<String> letterCombinations(String digits) {\r\n    \tList<String> result = new ArrayList<String>();\r\n    \tif(digits.length() == 0){\r\n            return new ArrayList<>(); \r\n        }\r\n    \t\r\n    \tfoo(digits, 0, 0, digits.length(), new ArrayList<Character>(), result);\r\n    \treturn result;\r\n    }\r\n    \r\n    public void foo(String digits, int index, int total, int len, List<Character> temp, List<String> result) {\r\n    \tif(total == len) {\r\n    \t\tStringBuilder stringBuilder = new StringBuilder();\r\n    \t\tfor(Character c: temp) {\r\n    \t\t\tstringBuilder.append(c);\r\n    \t\t}\r\n    \t\tresult.add(stringBuilder.toString());\r\n    \t\treturn;\r\n    \t}\r\n    \t\r\n    \tint numberIndex = Integer.parseInt(Character.toString(digits.charAt(index)));\r\n    \t\r\n    \tfor(char c: number[numberIndex - 2]) {\r\n    \t\ttemp.add(c);\r\n    \t\tfoo(digits, index+1, total+1, len, temp, result);\r\n    \t\ttemp.remove(temp.size() - 1);\r\n    \t}\r\n    }\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\t\/\/ TODO Auto-generated method stub\r\n\t\tLetter_Combinations_of_a_Phone_Number_17 s = new Letter_Combinations_of_a_Phone_Number_17();\r\n\t\tSystem.out.println(s.letterCombinations(\"234\"));\r\n\t}\r\n\r\n}\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>this is a very classic backtrace problem. Idea is simple, use &#8216;call stack&#8217; to store string &#8220;a&#8230;z&#8221; stack n &#8211; z . . stack 1 &#8211; a if stack.length == targetLenth loop though stack to get char array to string. package com.dw.leetcode; import java.util.*; public class Letter_Combinations_of_a_Phone_Number_17 { static final char[][] number = { { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-440","post","type-post","status-publish","format-standard","hentry","category-algorithm"],"_links":{"self":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=440"}],"version-history":[{"count":1,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/440\/revisions"}],"predecessor-version":[{"id":441,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/440\/revisions\/441"}],"wp:attachment":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=440"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}