Class Solution

java.lang.Object
g1601_1700.s1694_reformat_phone_number.Solution

public class Solution extends Object
1694 - Reformat Phone Number.<p>Easy</p> <p>You are given a phone number as a string <code>number</code>. <code>number</code> consists of digits, spaces <code>' '</code>, and/or dashes <code>'-'</code>.</p> <p>You would like to reformat the phone number in a certain manner. Firstly, <strong>remove</strong> all spaces and dashes. Then, <strong>group</strong> the digits from left to right into blocks of length 3 <strong>until</strong> there are 4 or fewer digits. The final digits are then grouped as follows:</p> <ul> <li>2 digits: A single block of length 2.</li> <li>3 digits: A single block of length 3.</li> <li>4 digits: Two blocks of length 2 each.</li> </ul> <p>The blocks are then joined by dashes. Notice that the reformatting process should <strong>never</strong> produce any blocks of length 1 and produce <strong>at most</strong> two blocks of length 2.</p> <p>Return <em>the phone number after formatting.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> number = &ldquo;1-23-45 6&rdquo;</p> <p><strong>Output:</strong> &ldquo;123-456&rdquo;</p> <p><strong>Explanation:</strong> The digits are &ldquo;123456&rdquo;.t</p> <p>Step 1: There are more than 4 digits, so group the next 3 digits. The 1st block is &ldquo;123&rdquo;.</p> <p>Step 2: There are 3 digits remaining, so put them in a single block of length 3. The 2nd block is &ldquo;456&rdquo;.</p> <p>Joining the blocks gives &ldquo;123-456&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> number = &ldquo;123 4-567&rdquo;</p> <p><strong>Output:</strong> &ldquo;123-45-67&rdquo;</p> <p><strong>Explanation:</strong> The digits are &ldquo;1234567&rdquo;.</p> <p>Step 1: There are more than 4 digits, so group the next 3 digits. The 1st block is &ldquo;123&rdquo;.</p> <p>Step 2: There are 4 digits left, so split them into two blocks of length 2. The blocks are &ldquo;45&rdquo; and &ldquo;67&rdquo;.</p> <p>Joining the blocks gives &ldquo;123-45-67&rdquo;.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> number = &ldquo;123 4-5678&rdquo;</p> <p><strong>Output:</strong> &ldquo;123-456-78&rdquo;</p> <p><strong>Explanation:</strong> The digits are &ldquo;12345678&rdquo;.</p> <p>Step 1: The 1st block is &ldquo;123&rdquo;.</p> <p>Step 2: The 2nd block is &ldquo;456&rdquo;.</p> <p>Step 3: There are 2 digits left, so put them in a single block of length 2. The 3rd block is &ldquo;78&rdquo;.</p> <p>Joining the blocks gives &ldquo;123-456-78&rdquo;.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= number.length <= 100</code></li> <li><code>number</code> consists of digits and the characters <code>'-'</code> and <code>' '</code>.</li> <li>There are at least <strong>two</strong> digits in <code>number</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • reformatNumber

      public String reformatNumber(String number)