[JAVA programming] letter combination of telephone number

Posted by closer on Fri, 04 Feb 2022 04:22:01 +0100

1, Title Description

Given a string containing only numbers 2-9, returns all the letter combinations it can represent. The answers can be returned in any order.
The mapping of numbers to letters is given as follows (the same as telephone keys). Note that 1 does not correspond to any letter.

Example 1:
Input: digits = "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]

Example 2:
Input: digits = ""
Output: []

Example 3:
Input: digits = "2"
Output: ["a", "b", "c"]

Tips:
0 <= digits.length <= 4
digits[i] is a number in the range ['2', '9'].

2, Problem analysis

According to the conditions of the input string, it can be divided by the length of the string.
When the length of the input number string is 0, it returns NULL;
When the length of the input number string is 1, the letter combination corresponding to the number is returned. For example, if the input is "9", it returns ["w", "x", "y", "z"). The code example is as follows:

String[] numstr = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
int index0;

if (digits.length() == 1) {
	index0 = digits.charAt(0) - '2';
	for (int i = 0; i < numstr[index0].length(); i++) {
		onestr.add(Character.toString(numstr[index0].charAt(i)));
	}
}

When the length of the input number string is 2, the letters corresponding to the two numbers are returned in full arrangement. For example, when the input is "67", the letters are returned in full arrangement as ["mp", "mq", "mr", "ms", "np", "nq", "nr", "ns", "op", "oq", "or", "os]. The code example is as follows:

String[] numstr = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
int index0, index1;

if (digits.length() >= 2) {
	index0 = digits.charAt(0) - '2';
	index1 = digits.charAt(1) - '2';
	for (int i = 0; i < numstr[index0].length(); i++) {
		for (int j = 0; j < numstr[index1].length(); j++) {
			onestr.add(Character.toString(numstr[index0].charAt(i)) + Character.toString(numstr[index1].charAt(j)));
		}
	}
}

When the length of the input number string is 3, the letters corresponding to the three numbers are returned in full arrangement. When the code is implemented, the three-layer cycle can be adopted to cycle through the letters corresponding to the three numbers for full arrangement. However, in order to reduce the cycle complexity, the letters of the first two numbers can be fully arranged with the letters of the next number.
For example, when the input is "678", the arrangement result corresponding to "67" can be taken, and the letters corresponding to "8" can be arranged and combined twice to obtain the final result.

When the length of the input number string is 4, the letters corresponding to the four numbers are returned in full arrangement. When the code is implemented, the letters of the first two numbers and the letters of the next two numbers can be implemented in full arrangement.
For example, when the input is "6789", you can take the arrangement result corresponding to "67" and arrange and combine the letters corresponding to "89" twice to get the final result.

The corresponding code is implemented as follows:

String[] numstr = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
int index0, index1;

if (digits.length() >= 2) {
	index0 = digits.charAt(0) - '2';
	index1 = digits.charAt(1) - '2';
	for (int i = 0; i < numstr[index0].length(); i++) {
		for (int j = 0; j < numstr[index1].length(); j++) {
			onestr.add(Character.toString(numstr[index0].charAt(i)) + Character.toString(numstr[index1].charAt(j)));
		}
	}
}
if (digits.length() == 3) {
	index0 = digits.charAt(2) - '2';
	for (int i = 0; i < numstr[index0].length(); i++) {
		twostr.add(Character.toString(numstr[index0].charAt(i)));
	}
} else if (digits.length() == 4) {
	index0 = digits.charAt(2) - '2';
	index1 = digits.charAt(3) - '2';
	for (int i = 0; i < numstr[index0].length(); i++) {
		for (int j = 0; j < numstr[index1].length(); j++) {
			twostr.add(Character.toString(numstr[index0].charAt(i)) + Character.toString(numstr[index1].charAt(j)));
		}
	}
}
// Take the first two digital results and the last one / two digital results for full arrangement
if (digits.length() > 2) {
	for (int i = 0; i < onestr.size(); i++) {
		for (int j = 0; j < twostr.size(); j++) {
			retstr.add(onestr.get(i) + twostr.get(j));
		}
	}
	return retstr;
}

3, Code implementation

The complete code is implemented as follows:

static public List<String> letterCombinations(String digits) {
   	List<String> retstr = new ArrayList<>();
   	List<String> onestr = new ArrayList<>();
   	List<String> twostr = new ArrayList<>();
   	String[] numstr = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
   	int index0, index1;
   	
   	if (digits.length() == 1) {
   		index0 = digits.charAt(0) - '2';
   		for (int i = 0; i < numstr[index0].length(); i++) {
   			onestr.add(Character.toString(numstr[index0].charAt(i)));
   		}
   	} else if (digits.length() >= 2) {
   		index0 = digits.charAt(0) - '2';
   		index1 = digits.charAt(1) - '2';
   		for (int i = 0; i < numstr[index0].length(); i++) {
   			for (int j = 0; j < numstr[index1].length(); j++) {
   				onestr.add(Character.toString(numstr[index0].charAt(i)) + Character.toString(numstr[index1].charAt(j)));
   			}
   		}
   	}
   	
   	if (digits.length() == 3) {
   		index0 = digits.charAt(2) - '2';
   		for (int i = 0; i < numstr[index0].length(); i++) {
   			twostr.add(Character.toString(numstr[index0].charAt(i)));
   		}
   	} else if (digits.length() == 4) {
   		index0 = digits.charAt(2) - '2';
   		index1 = digits.charAt(3) - '2';
   		for (int i = 0; i < numstr[index0].length(); i++) {
   			for (int j = 0; j < numstr[index1].length(); j++) {
   				twostr.add(Character.toString(numstr[index0].charAt(i)) + Character.toString(numstr[index1].charAt(j)));
   			}
   		}
   	}
   	// Take the first two digital results and the last one / two digital results for full arrangement
   	if (digits.length() > 2) {
   		for (int i = 0; i < onestr.size(); i++) {
   			for (int j = 0; j < twostr.size(); j++) {
   				retstr.add(onestr.get(i) + twostr.get(j));
   			}
   		}
   		return retstr;
   	}
   	return onestr;
}

Topics: Java leetcode