1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00
LeetCode/java/sum-of-digits-of-string-after-convert.java

24 lines
457 B
Java
Raw Normal View History

class Solution {
public int getLucky(String s, int k) {
// First iteration
int lucky = 0;
for (var c : s.toCharArray()) {
var number = c - 'a' + 1;
while (number > 0) {
lucky += number % 10;
number /= 10;
}
}
for (int i = 1; i < k; ++i) {
var nextLucky = 0;
for (; lucky > 0; lucky /= 10) {
nextLucky += lucky % 10;
}
lucky = nextLucky;
}
return lucky;
}
}