g1801_1900.s1844_replace_all_digits_with_characters.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java17 Show documentation
Show all versions of leetcode-in-java17 Show documentation
Java Solution for LeetCode algorithm problems, continually updating
package g1801_1900.s1844_replace_all_digits_with_characters;
// #Easy #String #2022_05_07_Time_1_ms_(70.12%)_Space_42.3_MB_(43.95%)
public class Solution {
public String replaceDigits(String s) {
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
if (Character.isAlphabetic(c)) {
sb.append(c);
} else {
sb.append((char) (sb.charAt(sb.length() - 1) + Character.getNumericValue(c)));
}
}
return sb.toString();
}
}