Leetcode Leetcode mock question 1694. Reformat phone number

CSDN Topic Challenge Phase 2
Participation Topic: Algorithm Solution

insert image description here
2022. October first day of check-in

Topic link and description

https://leetcode.cn/problems/reformat-phone-number/

Gives you a phone number number as a string. number consists of digits, spaces ' ', and dashes '-'.

Please reformat the phone number as follows.

First, remove all spaces and dashes.
Second, block the array in groups of 3 from left to right until 4 or fewer numbers remain. The remaining numbers will be sub-blocked as follows:
2 numbers: a single block of 2 numbers.
3 Numbers: A single block of 3 numbers.
4 Numbers: Two blocks of 2 numbers each.
Finally connect the blocks with dashes. Note that reformatting should not generate only 1-number blocks, and at most two 2-number blocks.

Returns the formatted phone number.

Example 1:

Input: number = "1-23-45 6"
Output: "123-456"
Explanation: The number is "123456"
Step 1: There are more than 4 numbers, so first take 3 numbers and group them together. The first block is "123".
Step 2: With 3 numbers left, put them into a single 3-number block. The 2nd block is "456".
After connecting these blocks you get "123-456".
Example 2:

Input: number = "123 4-567"
Output: "123-45-67"
Explanation: The number is "1234567".
Step 1: There are more than 4 numbers, so first take 3 numbers and group them together. The first block is "123".
Step 2: There are 4 numbers left, so divide them into two blocks of 2 numbers. The 2 blocks are "45" and "67" respectively.
After connecting these blocks you get "123-45-67".
Example 3:

Input: number = "123 4-5678"
Output: "123-456-78"
Explanation: The number is "12345678".
Step 1: The first block "123".
Step 2: The second block "456".
Step 3: With 2 numbers left, put them into a single 2-number block. The 3rd block is "78".
After connecting these blocks you get "123-456-78".
Example 4:

Input: number = "12"
Output: "12"
Example 5:

Input: number = "–17-5 229 35-39475"
Output: "175-229-353-94-75"

hint:

2 <= number.length <= 100
number consists of digits and characters '-' and ' '.
number contains at least 2 numbers.

Keywords: simulation questions

According to the meaning of the title:

  • More than 4, in groups of three
  • 4 divided into two groups
  • set of 3
  • 2 sets

method one:

run screenshot

insert image description here

the code



	public String reformatNumber(String number) {
    
    
		number = number.replace("-", "").replace(" ", "");
		int length = number.length();
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < length; ) {
    
    
			if (length - i > 4) {
    
    
				stringBuilder.append(number, i, i + 3).append('-');
				i += 3;
			} else if (length - i == 4) {
    
    
				stringBuilder.append(number, i, i + 2).append('-').append(number, i + 2, i + 4);
				i += 4;
			} else if (length - i == 3) {
    
    
				stringBuilder.append(number, i, i + 3);
				i += 3;
			} else if (length - i == 2) {
    
    
				stringBuilder.append(number, i, i + 2);
				i += 2;
			}
		}
		return stringBuilder.toString();
	}

end

Welcome to communicate in the comment area, check in every day, and rush! ! !

Guess you like

Origin blog.csdn.net/qq_35530042/article/details/127137219