|
| 1 | +/***************************************************************************************************** |
| 2 | + * |
| 3 | + * You are given a string num, which represents a large integer. You are also given a 0-indexed |
| 4 | + * integer array change of length 10 that maps each digit 0-9 to another digit. More formally, digit d |
| 5 | + * maps to digit change[d]. |
| 6 | + * |
| 7 | + * You may choose to mutate a single substring of num. To mutate a substring, replace each digit |
| 8 | + * num[i] with the digit it maps to in change (i.e. replace num[i] with change[num[i]]). |
| 9 | + * |
| 10 | + * Return a string representing the largest possible integer after mutating (or choosing not to) a |
| 11 | + * single substring of num. |
| 12 | + * |
| 13 | + * A substring is a contiguous sequence of characters within the string. |
| 14 | + * |
| 15 | + * Example 1: |
| 16 | + * |
| 17 | + * Input: num = "132", change = [9,8,5,0,3,6,4,2,6,8] |
| 18 | + * Output: "832" |
| 19 | + * Explanation: Replace the substring "1": |
| 20 | + * - 1 maps to change[1] = 8. |
| 21 | + * Thus, "132" becomes "832". |
| 22 | + * "832" is the largest number that can be created, so return it. |
| 23 | + * |
| 24 | + * Example 2: |
| 25 | + * |
| 26 | + * Input: num = "021", change = [9,4,3,5,7,2,1,9,0,6] |
| 27 | + * Output: "934" |
| 28 | + * Explanation: Replace the substring "021": |
| 29 | + * - 0 maps to change[0] = 9. |
| 30 | + * - 2 maps to change[2] = 3. |
| 31 | + * - 1 maps to change[1] = 4. |
| 32 | + * Thus, "021" becomes "934". |
| 33 | + * "934" is the largest number that can be created, so return it. |
| 34 | + * |
| 35 | + * Example 3: |
| 36 | + * |
| 37 | + * Input: num = "5", change = [1,4,7,5,3,2,5,6,9,4] |
| 38 | + * Output: "5" |
| 39 | + * Explanation: "5" is already the largest number that can be created, so return it. |
| 40 | + * |
| 41 | + * Constraints: |
| 42 | + * |
| 43 | + * 1 <= num.length <= 10^5 |
| 44 | + * num consists of only digits 0-9. |
| 45 | + * change.length == 10 |
| 46 | + * 0 <= change[d] <= 9 |
| 47 | + ******************************************************************************************************/ |
| 48 | + |
| 49 | +class Solution { |
| 50 | + public String maximumNumber(String num, int[] change) { |
| 51 | + |
| 52 | + //make flag for checking replacement |
| 53 | + boolean shouldReplace = false; |
| 54 | + |
| 55 | + //work through substring |
| 56 | + for(int i = 0; i < num.length(); i++){ |
| 57 | + |
| 58 | + //subtract zero to get correct ascii conversion |
| 59 | + char a = num.charAt(i); |
| 60 | + char curr = (char)(a - '0'); |
| 61 | + |
| 62 | + if(curr < change[curr]){ |
| 63 | + |
| 64 | + char c = (char)(change[curr] + '0'); |
| 65 | + |
| 66 | + num = num.replace(num.charAt(i), c); |
| 67 | + |
| 68 | + shouldReplace = true; |
| 69 | + |
| 70 | + }else if((curr > change[curr]) && (shouldReplace)){ |
| 71 | + |
| 72 | + break; |
| 73 | + |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return num; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | + |
0 commit comments