Skip to content

Commit 12285b1

Browse files
committedDec 22, 2021
solvesreplace all digitswith characters
1 parent 514bfc1 commit 12285b1

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed
 

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@
447447
| 1827 | [Minimum Operations to Make the Array Increasing](https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing) | [![Java](assets/java.png)](src/MinimumOperationsToMakeTheArrayIncreasing.java) | |
448448
| 1832 | [Check if the Sentence Is Pangram](https://leetcode.com/problems/check-if-the-sentence-is-pangram) | [![Java](assets/java.png)](src/CheckIfSentenceIsPangram.java) | |
449449
| 1837 | [Sum of Digits in Base K](https://leetcode.com/problems/sum-of-digits-in-base-k) | [![Java](assets/java.png)](src/SumOfDigitsInBaseK.java) | |
450-
| 1844 | [Replace All Digits with Characters](https://leetcode.com/problems/replace-all-digits-with-characters) | | |
450+
| 1844 | [Replace All Digits with Characters](https://leetcode.com/problems/replace-all-digits-with-characters) | [![Java](assets/java.png)](src/ReplaceAllDigitsWithCharacters.java) | |
451451
| 1848 | [Minimum Distance to the Target Element](https://leetcode.com/problems/minimum-distance-to-the-target-element) | | |
452452
| 1854 | [Maximum Population Year](https://leetcode.com/problems/maximum-population-year) | | |
453453
| 1859 | [Sorting the Sentence](https://leetcode.com/problems/sorting-the-sentence) | | |
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class ReplaceAllDigitsWithCharacters {
2+
public String replaceDigits(String s) {
3+
final StringBuilder result = new StringBuilder();
4+
for (int i = 1 ; i < s.length() ; i += 2) {
5+
result.append(s.charAt(i - 1)).append(shift(s.charAt(i - 1), s.charAt(i) - '0'));
6+
}
7+
if (s.length() % 2 == 1) {
8+
result.append(s.charAt(s.length() - 1));
9+
}
10+
return result.toString();
11+
}
12+
13+
private char shift(char c, int shiftAmount) {
14+
return (char) (c + shiftAmount);
15+
}
16+
}

0 commit comments

Comments
 (0)
Please sign in to comment.