From 637015090a62063ce21e3b42f4aeb662f0edcacc Mon Sep 17 00:00:00 2001 From: Will <73602111+willmccarten@users.noreply.github.com> Date: Mon, 31 Jan 2022 03:06:01 -0500 Subject: [PATCH] New java solution to problem 1946 and updated readme (#268) * Create Java solution for problem 1946 ran fine on leetcode, but was having some potential issues with unwanted non printable ascii characters * update readme for java solution on 1946 --- README.md | 2 +- .../largestNumberAfterMutatingSubstring.java | 81 +++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 algorithms/java/src/LargestNumberAfterMutatingSubstring/largestNumberAfterMutatingSubstring.java diff --git a/README.md b/README.md index 4cbdb6ebd..d3d78a84d 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ LeetCode | # | Title | Solution | Difficulty | |---| ----- | -------- | ---------- | -|1946|[Largest Number After Mutating Substring](https://leetcode.com/problems/largest-number-after-mutating-substring/) | [C++](./algorithms/cpp/largestNumberAfterMutatingSubstring/LargestNumberAfterMutatingSubstring.cpp)|Medium| +|1946|[Largest Number After Mutating Substring](https://leetcode.com/problems/largest-number-after-mutating-substring/) | [C++](./algorithms/cpp/largestNumberAfterMutatingSubstring/LargestNumberAfterMutatingSubstring.cpp), [Java](./algorithms/java/src/LargestNumberAfterMutatingSubtring/largestNumberAfterMutatingSubstring.java)|Medium| |1945|[Sum of Digits of String After Convert](https://leetcode.com/problems/sum-of-digits-of-string-after-convert/) | [C++](./algorithms/cpp/leetcode/sumOfDigitsOfStringAfterConvert/SumOfDigitsOfStringAfterConvert.cpp)|Easy| |1935|[Maximum Number of Words You Can Type](https://leetcode.com/problems/maximum-number-of-words-you-can-type/) | [C++](./algorithms/cpp/maximumNumberOfWordsYouCanType/MaximumNumberOfWordsYouCanType.cpp)|Easy| |1884|[Egg Drop With 2 Eggs and N Floors](https://leetcode.com/problems/egg-drop-with-2-eggs-and-n-floors/) | [C++](./algorithms/cpp/eggDropWith2EggsAndNFloors/EggDropWith2EggsAndNFloors.cpp)|Medium| diff --git a/algorithms/java/src/LargestNumberAfterMutatingSubstring/largestNumberAfterMutatingSubstring.java b/algorithms/java/src/LargestNumberAfterMutatingSubstring/largestNumberAfterMutatingSubstring.java new file mode 100644 index 000000000..ae4d53dff --- /dev/null +++ b/algorithms/java/src/LargestNumberAfterMutatingSubstring/largestNumberAfterMutatingSubstring.java @@ -0,0 +1,81 @@ +/***************************************************************************************************** + * + * You are given a string num, which represents a large integer. You are also given a 0-indexed + * integer array change of length 10 that maps each digit 0-9 to another digit. More formally, digit d + * maps to digit change[d]. + * + * You may choose to mutate a single substring of num. To mutate a substring, replace each digit + * num[i] with the digit it maps to in change (i.e. replace num[i] with change[num[i]]). + * + * Return a string representing the largest possible integer after mutating (or choosing not to) a + * single substring of num. + * + * A substring is a contiguous sequence of characters within the string. + * + * Example 1: + * + * Input: num = "132", change = [9,8,5,0,3,6,4,2,6,8] + * Output: "832" + * Explanation: Replace the substring "1": + * - 1 maps to change[1] = 8. + * Thus, "132" becomes "832". + * "832" is the largest number that can be created, so return it. + * + * Example 2: + * + * Input: num = "021", change = [9,4,3,5,7,2,1,9,0,6] + * Output: "934" + * Explanation: Replace the substring "021": + * - 0 maps to change[0] = 9. + * - 2 maps to change[2] = 3. + * - 1 maps to change[1] = 4. + * Thus, "021" becomes "934". + * "934" is the largest number that can be created, so return it. + * + * Example 3: + * + * Input: num = "5", change = [1,4,7,5,3,2,5,6,9,4] + * Output: "5" + * Explanation: "5" is already the largest number that can be created, so return it. + * + * Constraints: + * + * 1 <= num.length <= 10^5 + * num consists of only digits 0-9. + * change.length == 10 + * 0 <= change[d] <= 9 + ******************************************************************************************************/ + +class Solution { + public String maximumNumber(String num, int[] change) { + + //make flag for checking replacement + boolean shouldReplace = false; + + //work through substring + for(int i = 0; i < num.length(); i++){ + + //subtract zero to get correct ascii conversion + char a = num.charAt(i); + char curr = (char)(a - '0'); + + if(curr < change[curr]){ + + char c = (char)(change[curr] + '0'); + + num = num.replace(num.charAt(i), c); + + shouldReplace = true; + + }else if((curr > change[curr]) && (shouldReplace)){ + + break; + + } + } + + return num; + } +} + +