Skip to content

Commit 6370150

Browse files
authored
New java solution to problem 1946 and updated readme (haoel#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
1 parent e00c0f6 commit 6370150

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LeetCode
99

1010
| # | Title | Solution | Difficulty |
1111
|---| ----- | -------- | ---------- |
12-
|1946|[Largest Number After Mutating Substring](https://leetcode.com/problems/largest-number-after-mutating-substring/) | [C++](./algorithms/cpp/largestNumberAfterMutatingSubstring/LargestNumberAfterMutatingSubstring.cpp)|Medium|
12+
|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|
1313
|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|
1414
|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|
1515
|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|
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)