1694. Reformat Phone Number Easy
1/**
2 * [1694] Reformat Phone Number
3 *
4 * You are given a phone number as a string number. number consists of digits, spaces ' ', and/or dashes '-'.
5 * You would like to reformat the phone number in a certain manner. Firstly, remove all spaces and dashes. Then, group the digits from left to right into blocks of length 3 until there are 4 or fewer digits. The final digits are then grouped as follows:
6 *
7 * 2 digits: A single block of length 2.
8 * 3 digits: A single block of length 3.
9 * 4 digits: Two blocks of length 2 each.
10 *
11 * The blocks are then joined by dashes. Notice that the reformatting process should never produce any blocks of length 1 and produce at most two blocks of length 2.
12 * Return the phone number after formatting.
13 *
14 * Example 1:
15 *
16 * Input: number = "1-23-45 6"
17 * Output: "123-456"
18 * Explanation: The digits are "123456".
19 * Step 1: There are more than 4 digits, so group the next 3 digits. The 1st block is "123".
20 * Step 2: There are 3 digits remaining, so put them in a single block of length 3. The 2nd block is "456".
21 * Joining the blocks gives "123-456".
22 *
23 * Example 2:
24 *
25 * Input: number = "123 4-567"
26 * Output: "123-45-67"
27 * Explanation: The digits are "1234567".
28 * Step 1: There are more than 4 digits, so group the next 3 digits. The 1st block is "123".
29 * Step 2: There are 4 digits left, so split them into two blocks of length 2. The blocks are "45" and "67".
30 * Joining the blocks gives "123-45-67".
31 *
32 * Example 3:
33 *
34 * Input: number = "123 4-5678"
35 * Output: "123-456-78"
36 * Explanation: The digits are "12345678".
37 * Step 1: The 1st block is "123".
38 * Step 2: The 2nd block is "456".
39 * Step 3: There are 2 digits left, so put them in a single block of length 2. The 3rd block is "78".
40 * Joining the blocks gives "123-456-78".
41 *
42 *
43 * Constraints:
44 *
45 * 2 <= number.length <= 100
46 * number consists of digits and the characters '-' and ' '.
47 * There are at least two digits in number.
48 *
49 */
50pub struct Solution {}
51
52// problem: https://leetcode.com/problems/reformat-phone-number/
53// discuss: https://leetcode.com/problems/reformat-phone-number/discuss/?currentPage=1&orderBy=most_votes&query=
54
55// submission codes start here
56
57impl Solution {
58 pub fn reformat_number(number: String) -> String {
59 String::new()
60 }
61}
62
63// submission codes end
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[test]
70 fn test_1694() {
71 }
72}
73
Back
© 2025 bowen.ge All Rights Reserved.