This content originally appeared on DEV Community and was authored by MD ARIFUL HAQUE
1945. Sum of Digits of String After Convert
Difficulty: Easy
Topics: String
, Simulation
You are given a string s
consisting of lowercase English letters, and an integer k
.
First, convert s
into an integer by replacing each letter with its position in the alphabet (i.e., replace 'a'
with 1
, 'b'
with 2
, ..., 'z'
with 26
). Then, transform the integer by replacing it with the sum of its digits. Repeat the transform operation k
times in total.
For example, if s = "zbax"
and k = 2
, then the resulting integer would be 8
by the following operations:
-
Convert:
"zbax" ➝ "(26)(2)(1)(24)" ➝ "262124" ➝ 262124
-
Transform #1:
262124 ➝ 2 + 6 + 2 + 1 + 2 + 4 ➝ 17
-
Transform #2:
17 ➝ 1 + 7 ➝ 8
Return the resulting integer after performing the operations described above.
Example 1:
- Input: s = "iiii", k = 1
- Output: 36
-
Explanation: The operations are as follows:
- Convert: "iiii" ➝ "(9)(9)(9)(9)" ➝ "9999" ➝ 9999
- Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36
- Thus the resulting integer is 36.
Example 2:
- Input: s = "leetcode", k = 2
- Output: 6
-
Explanation: The operations are as follows:
- Convert: "leetcode" ➝ "(12)(5)(5)(20)(3)(15)(4)(5)" ➝ "12552031545" ➝ 12552031545
- Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33
- Transform #2: 33 ➝ 3 + 3 ➝ 6
- Thus the resulting integer is 6.
Example 3:
- Input: s = "zbax", k = 2
- Output: 8
Constraints:
1 <= s.length <= 100
1 <= k <= 10
-
s
consists of lowercase English letters.
Hint:
- First, let's note that after the first transform the value will be at most
100 * 10
which is not much - After The first transform, we can just do the rest of the transforms by brute force
Solution:
We can break down the solution into two main steps:
-
Convert the string
s
into an integer:- Each character in the string is replaced with its corresponding position in the alphabet (e.g., 'a' -> 1, 'b' -> 2, ..., 'z' -> 26).
- Concatenate all these numbers to form a large integer.
-
Transform the integer by summing its digits
k
times:- For each transformation, sum all the digits of the current number.
- Repeat this transformation process
k
times.
Let's implement this solution in PHP: 1945. Sum of Digits of String After Convert
<?php
function getLucky($s, $k) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo getLucky("iiii", 1) . "\n"; // Output: 36
echo getLucky("leetcode", 2) . "\n"; // Output: 6
echo getLucky("zbax", 2) . "\n"; // Output: 8
?>
Explanation:
-
Convert the String:
- We loop through each character in the string
s
and calculate its corresponding alphabet position usingord($s[$i]) - ord('a') + 1
. - These values are concatenated to form a large string
numStr
representing the number.
- We loop through each character in the string
-
Transform the Number:
- We loop
k
times, each time summing the digits of the currentnumStr
. - The result of this summing operation is stored back in
numStr
as a string to allow for further transformations. - After
k
transformations, we return the final integer value.
- We loop
Test Cases:
-
"iiii"
withk = 1
converts to"9999"
, sums to36
, and sincek=1
, the final result is36
. -
"leetcode"
withk = 2
converts to"12552031545"
, sums to33
in the first transform, and to6
in the second transform, resulting in6
. -
"zbax"
withk = 2
converts to"262124"
, sums to17
in the first transform, and to8
in the second transform, resulting in8
.
This solution is efficient given the constraints and will work well within the provided limits.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
This content originally appeared on DEV Community and was authored by MD ARIFUL HAQUE
MD ARIFUL HAQUE | Sciencx (2024-09-03T19:42:14+00:00) 1945. Sum of Digits of String After Convert. Retrieved from https://www.scien.cx/2024/09/03/1945-sum-of-digits-of-string-after-convert/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.