class Solution:
def getLucky(self, s: str, k: int) -> int:
# Step 1: Convert each character to its alphabet position and concatenate
num_str = ''.join(str(ord(c) - ord('a') + 1) for c in s)
# Step 2: Repeat k times: sum the digits
for _ in range(k):
num_str = str(sum(int(d) for d in num_str))
return int(num_str)
class Solution {
public:
int getLucky(string s, int k) {
string num_str = "";
for (char c : s) {
num_str += to_string(c - 'a' + 1);
}
for (int i = 0; i < k; ++i) {
int sum = 0;
for (char d : num_str) {
sum += d - '0';
}
num_str = to_string(sum);
}
return stoi(num_str);
}
};
class Solution {
public int getLucky(String s, int k) {
StringBuilder numStr = new StringBuilder();
for (char c : s.toCharArray()) {
numStr.append((int)(c - 'a' + 1));
}
String str = numStr.toString();
for (int i = 0; i < k; i++) {
int sum = 0;
for (char d : str.toCharArray()) {
sum += d - '0';
}
str = Integer.toString(sum);
}
return Integer.parseInt(str);
}
}
var getLucky = function(s, k) {
let numStr = '';
for (let i = 0; i < s.length; i++) {
numStr += (s.charCodeAt(i) - 'a'.charCodeAt(0) + 1).toString();
}
for (let i = 0; i < k; i++) {
let sum = 0;
for (let d of numStr) {
sum += Number(d);
}
numStr = sum.toString();
}
return Number(numStr);
};
Given a string s
consisting of lowercase English letters and an integer k
, your task is to perform the following operations:
s
with its position in the alphabet (i.e., 'a' becomes 1, 'b' becomes 2, ..., 'z' becomes 26). Concatenate these numbers to form a new string of digits.
For example, with s = "zbax"
and k = 2
:
8
.
To solve this problem, we need to break it into two main parts: character-to-number conversion and repeated digit summing. The first step is straightforward—map each character to its corresponding alphabet position and concatenate the results. The second step is to sum the digits of the resulting number k
times.
At first glance, you might think of handling each letter separately or using arrays to store the positions, but since the problem asks for digit sums, it's more efficient to work with string representations of numbers. This avoids issues with large numbers and makes digit summing easy.
Brute-force would involve converting, then summing the digits in a loop, but it's already efficient since the number shrinks quickly. There's no need for advanced optimization; just careful implementation.
c
in s
, compute ord(c) - ord('a') + 1
(or equivalent in other languages).k
iterations, sum all digits in the current string.k
transformations are complete.k
digit sums, the string will be a small number. Return it as an integer.This approach is simple and leverages string operations for easy digit access. No extra data structures are needed.
Let's walk through an example with s = "abc"
and k = 2
:
This matches the expected output.
s
.k
times: O(k * m), but m
shrinks quickly after each iteration.
The solution is a straightforward application of string and digit manipulation. By converting each letter to its alphabet position and concatenating, then summing digits k
times, we can efficiently solve the problem. The key insight is to treat the number as a string for easy digit access, and recognize that the digit sum process quickly reduces the size of the number, keeping the algorithm efficient and simple.