|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +/** |
| 7 | + * 1417. Reformat The String |
| 8 | + * |
| 9 | + * Given alphanumeric string s. (Alphanumeric string is a string consisting of lowercase English letters and digits). |
| 10 | + * You have to find a permutation of the string where no letter is followed by another letter and no digit is followed by another digit. |
| 11 | + * That is, no two adjacent characters have the same type. |
| 12 | + * Return the reformatted string or return an empty string if it is impossible to reformat the string. |
| 13 | + * |
| 14 | + * Example 1: |
| 15 | + * Input: s = "a0b1c2" |
| 16 | + * Output: "0a1b2c" |
| 17 | + * Explanation: No two adjacent characters have the same type in "0a1b2c". "a0b1c2", "0a1b2c", "0c2a1b" are also valid permutations. |
| 18 | + * |
| 19 | + * Example 2: |
| 20 | + * Input: s = "leetcode" |
| 21 | + * Output: "" |
| 22 | + * Explanation: "leetcode" has only characters so we cannot separate them by digits. |
| 23 | + * |
| 24 | + * Example 3: |
| 25 | + * Input: s = "1229857369" |
| 26 | + * Output: "" |
| 27 | + * Explanation: "1229857369" has only digits so we cannot separate them by characters. |
| 28 | + * |
| 29 | + * Example 4: |
| 30 | + * Input: s = "covid2019" |
| 31 | + * Output: "c2o0v1i9d" |
| 32 | + * |
| 33 | + * Example 5: |
| 34 | + * Input: s = "ab123" |
| 35 | + * Output: "1a2b3" |
| 36 | + * |
| 37 | + * Constraints: |
| 38 | + * 1 <= s.length <= 500 |
| 39 | + * s consists of only lowercase English letters and/or digits. |
| 40 | + * */ |
| 41 | +public class _1417 { |
| 42 | + public static class Solution1 { |
| 43 | + public String reformat(String s) { |
| 44 | + List<Character> characterList = new ArrayList<>(); |
| 45 | + List<Character> numberList = new ArrayList<>(); |
| 46 | + for (char c : s.toCharArray()) { |
| 47 | + if (Character.isAlphabetic(c)) { |
| 48 | + characterList.add(c); |
| 49 | + } else { |
| 50 | + numberList.add(c); |
| 51 | + } |
| 52 | + } |
| 53 | + if (Math.abs(characterList.size() - numberList.size()) > 1) { |
| 54 | + return ""; |
| 55 | + } else { |
| 56 | + StringBuilder sb = new StringBuilder(); |
| 57 | + if (characterList.size() > numberList.size()) { |
| 58 | + for (int i = 0; i < characterList.size() - 1; i++) { |
| 59 | + sb.append(characterList.get(i)); |
| 60 | + sb.append(numberList.get(i)); |
| 61 | + } |
| 62 | + sb.append(characterList.get(characterList.size() - 1)); |
| 63 | + } else if (characterList.size() == numberList.size()) { |
| 64 | + for (int i = 0; i < numberList.size(); i++) { |
| 65 | + sb.append(numberList.get(i)); |
| 66 | + sb.append(characterList.get(i)); |
| 67 | + } |
| 68 | + } else { |
| 69 | + for (int i = 0; i < numberList.size() - 1; i++) { |
| 70 | + sb.append(numberList.get(i)); |
| 71 | + sb.append(characterList.get(i)); |
| 72 | + } |
| 73 | + sb.append(numberList.get(numberList.size() - 1)); |
| 74 | + } |
| 75 | + return sb.toString(); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments