diff --git a/src/test/java/com/ossez/lang/tutorial/tests/lintcode/LintCode0425LetterCombinationsTest.java b/src/test/java/com/ossez/lang/tutorial/tests/lintcode/LintCode0425LetterCombinationsTest.java new file mode 100644 index 0000000000..8c6c281cb3 --- /dev/null +++ b/src/test/java/com/ossez/lang/tutorial/tests/lintcode/LintCode0425LetterCombinationsTest.java @@ -0,0 +1,77 @@ +package com.ossez.lang.tutorial.tests.lintcode; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + *

+ * 425 + *

+ *

+ * + * @author YuCheng + * + */ +public class LintCode0425LetterCombinationsTest { + + private final static Logger logger = LoggerFactory.getLogger(LintCode0425LetterCombinationsTest.class); + + /** + * + */ + @Test + public void testMain() { + logger.debug("LetterCombinationsTest"); + String digits = "23"; + + HashMap phoneKeyMap = new HashMap(); + phoneKeyMap.put("0", ""); + phoneKeyMap.put("1", ""); + phoneKeyMap.put("2", "abc"); + phoneKeyMap.put("3", "def"); + phoneKeyMap.put("4", "ghi"); + phoneKeyMap.put("5", "jkl"); + phoneKeyMap.put("6", "mno"); + phoneKeyMap.put("7", "pqrs"); + phoneKeyMap.put("8", "tuv"); + phoneKeyMap.put("9", "wxyz"); + + List retStatus = new ArrayList<>(); + + if (digits != null && digits.length() != 0) { + phoneRecursive(digits, retStatus, phoneKeyMap, "", 0); + } + + System.out.println(retStatus); + } + + /** + * phoneRecursive + * + * @param digits + * @param retStatus + * @param phoneKeyMap + * @param comb + * @param index + */ + private void phoneRecursive(String digits, List retStatus, HashMap phoneKeyMap, String comb, int index) { + if (index == digits.length()) { + retStatus.add(comb); + return; + } + + char pos = digits.charAt(index); + for (char c : ((String) phoneKeyMap.get(String.valueOf(pos))).toCharArray()) { + phoneRecursive(digits, retStatus, phoneKeyMap, comb + c, index + 1); + } + } +}