Skip to content

Commit

Permalink
my solution for 1945
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremymanning authored Sep 3, 2024
1 parent a238aeb commit 0d6b3bc
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions problems/1945/jeremymanning.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
# [Problem 1945: Sum of Digits of String After Convert](https://leetcode.com/problems/sum-of-digits-of-string-after-convert/description/?envType=daily-question)

## Initial thoughts (stream-of-consciousness)
- Another easy one 🥳!
- Let's hard code in the lowercase letters (`letters = 'abcdefghijklmnopqrstuvwxyz'`)
- Then we can make a `letter2num` decoder: `letter2num = {c: str(i + 1) for i, c in enumerate(letters)}`
- Next, we just replace each letter with it's number, stitch it into a string, and convert to an integer: `num = int("".join([letter2num[c] for c in s]))`
- Finally, loop through `k` times to sum the digits:
```python
for _ in range(k):
num = str(sum([int(i) for i in str(num)]))
```
- And then we just return `int(num)`

## Refining the problem, round 2 thoughts
- There are no tricky edge cases, as far as I can tell; `k` is always at least 1, and the strings aren't super long so we don't need to worry about overflow issues
- Let's try it!


## Attempted solution(s)
```python
class Solution: # paste your code here!
...
class Solution:
def getLucky(self, s: str, k: int) -> int:
letters = 'abcdefghijklmnopqrstuvwxyz'
letter2num = {c: str(i + 1) for i, c in enumerate(letters)}

num = int("".join([letter2num[c] for c in s]))
for _ in range(k):
num = str(sum([int(i) for i in str(num)]))

return int(num)
```
- Given test cases pass
- Let's try some other random ones:
- `s = "asdlkoifhawioefuh", k = 5`: pass
- `s = "dkfjkjdfijshdeweskjhaskljdfhjsldkjf", k = 4`: pass
- I think we're good; submitting...

![Screenshot 2024-09-02 at 8 41 31 PM](https://github.com/user-attachments/assets/0a1d3f63-c163-4bfb-a8ec-286ee3b9c0cc)

Solved!

0 comments on commit 0d6b3bc

Please sign in to comment.