Skip to content

Commit 0391e52

Browse files
committed
New Problem Solution - "1945. Sum of Digits of String After Convert"
1 parent cf08222 commit 0391e52

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LeetCode
99

1010
| # | Title | Solution | Difficulty |
1111
|---| ----- | -------- | ---------- |
12+
|1945|[Sum of Digits of String After Convert](https://leetcode.com/problems/sum-of-digits-of-string-after-convert/) | [C++](/Users/chenhao/GitHub/leetcode/sumOfDigitsOfStringAfterConvert/SumOfDigitsOfStringAfterConvert.cpp)|Easy|
1213
|1935|[Maximum Number of Words You Can Type](https://leetcode.com/problems/maximum-number-of-words-you-can-type/) | [C++](./algorithms/cpp/maximumNumberOfWordsYouCanType/MaximumNumberOfWordsYouCanType.cpp)|Easy|
1314
|1882|[Process Tasks Using Servers](https://leetcode.com/problems/process-tasks-using-servers/) | [C++](./algorithms/cpp/processTasksUsingServers/ProcessTasksUsingServers.cpp)|Medium|
1415
|1881|[Maximum Value after Insertion](https://leetcode.com/problems/maximum-value-after-insertion/) | [C++](./algorithms/cpp/maximumValueAfterInsertion/MaximumValueAfterInsertion.cpp)|Medium|
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Source : https://leetcode.com/problems/sum-of-digits-of-string-after-convert/
2+
// Author : Hao Chen
3+
// Date : 2021-11-12
4+
5+
/*****************************************************************************************************
6+
*
7+
* You are given a string s consisting of lowercase English letters, and an integer k.
8+
*
9+
* First, convert s into an integer by replacing each letter with its position in the alphabet (i.e.,
10+
* replace 'a' with 1, 'b' with 2, ..., 'z' with 26). Then, transform the integer by replacing it with
11+
* the sum of its digits. Repeat the transform operation k times in total.
12+
*
13+
* For example, if s = "zbax" and k = 2, then the resulting integer would be 8 by the following
14+
* operations:
15+
*
16+
* Convert: "zbax" ➝ "(26)(2)(1)(24)" ➝ "262124" ➝ 262124
17+
* Transform #1: 262124 ➝ 2 + 6 + 2 + 1 + 2 + 4 ➝ 17
18+
* Transform #2: 17 ➝ 1 + 7 ➝ 8
19+
*
20+
* Return the resulting integer after performing the operations described above.
21+
*
22+
* Example 1:
23+
*
24+
* Input: s = "iiii", k = 1
25+
* Output: 36
26+
* Explanation: The operations are as follows:
27+
* - Convert: "iiii" ➝ "(9)(9)(9)(9)" ➝ "9999" ➝ 9999
28+
* - Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36
29+
* Thus the resulting integer is 36.
30+
*
31+
* Example 2:
32+
*
33+
* Input: s = "leetcode", k = 2
34+
* Output: 6
35+
* Explanation: The operations are as follows:
36+
* - Convert: "leetcode" ➝ "(12)(5)(5)(20)(3)(15)(4)(5)" ➝ "12552031545" ➝ 12552031545
37+
* - Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33
38+
* - Transform #2: 33 ➝ 3 + 3 ➝ 6
39+
* Thus the resulting integer is 6.
40+
*
41+
* Example 3:
42+
*
43+
* Input: s = "zbax", k = 2
44+
* Output: 8
45+
*
46+
* Constraints:
47+
*
48+
* 1 <= s.length <= 100
49+
* 1 <= k <= 10
50+
* s consists of lowercase English letters.
51+
******************************************************************************************************/
52+
53+
class Solution {
54+
public:
55+
int sumChar(char c) {
56+
int x = c - 'a' + 1;
57+
return x < 10 ? x : x / 10 + x % 10;
58+
}
59+
int sumInt(int x) {
60+
int s = 0;
61+
while( x > 0 ) {
62+
s += x % 10;
63+
x /= 10;
64+
}
65+
return s;
66+
}
67+
int getLucky(string s, int k) {
68+
int result = 0;
69+
for (auto c : s) {
70+
result += sumChar(c);
71+
}
72+
for (; k > 1 ; k--) {
73+
result = sumInt(result);
74+
}
75+
return result;
76+
}
77+
};

0 commit comments

Comments
 (0)