Problem
letter mapping
a b c d e f g h i j k l m n o p q r s t u v w x y z
1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9
When each alphabet has each value as above, count the number of substring from the input string, such that the sum of the substrings is divisible by its length.
Write a function dataPacket(input).
Eaxmple 1.
Input "cat"
output 4
Explaination
c: sum=2, the sum is divisibly by length 1
a: sum=1, the sum is divisibly by length 1
t: sum=7, the sum is divisible by length 1
ca: sum=2+1, the sum is NOT divisible by length 2
cat: sum=2+1+7=10, the sum is NOT divisible by length 3
at: sum = 1+7=8, the sum is divisible by length 2
Example 2.
Input "hack"
output 7
Solution
At first, letter mapping should be defined.
You can hardcode this when you have enough time like below.
mapping = {'a':1, 'b':1, ...}
But, when you are in the coding test, there is not enough time.
I didn't know Python has this cool method `zip()`.
You can easily create a dictionary with 2 lists using `zip()` method.
mapping = dict(zip('abcdefghijklmnopqrstuvwxyz',[1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9]))
This way we can create our dictionary mapped with alphpabet and number value quickly.
And Now you want to know which value your input string has.
nums = [mapping[char] for char in input]
While iterating through given input strings, you take the corresponding value from the mapping dictionary and assign it to nums array.
The above syntax is Pythonic which is called as list comprehension.
[<expression> for <variable> in <iterable>]
<expression> is the resulting value.
This syntax is exactly the same as below code.
nums = []
for char in input:
nums.append(mapping(char))
With this, we've got ready with our value list.
input "hack"
nums [3, 1, 2, 4]
And then you look through this nums list if all substrings meet the condition. So, from the criteria index, check if the sum from the criteria index(1st loop) to the nested index value (2nd loop) meets whether its sum is divisible by its length.
Here's how it looks when the loop is iterating over.
"hack" → [3, 1, 2, 4]
Substring | Indices | Sum | Length | Sum % Length | Valid? |
h | [0:1] | 3 | 1 | 3/1 = 0 | โ |
ha | [0:2] | 3+1 = 4 | 2 | 4/2 = 0 | โ |
hac | [0:3] | 3+1+2 = 6 | 3 | 6/3 = 0 | โ |
hack | [0:4] | 3+1+2+4 = 10 | 4 | 10/4 | โ |
a | [1:2] | 1 | 1 | 1/1 = 0 | โ |
ac | [1:3] | 1+2 = 3 | 2 | 3/2 | โ |
ack | [1:4] | 1+2+4 = 7 | 3 | 7/3 | โ |
c | [2:3] | 2 | 1 | 2/1 | โ |
ck | [2:4] | 2+4 = 6 | 2 | 6/2 | โ |
k | [2:3] | 4 | 1 | 4/1 | โ |
When you accumulate your valid count while looping, you can return `count` at the end.
Here's a whole line of code.
mapping = dict(zip('abcdefghijklmnopqrstuvwxyz',[1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9]))
def dataPacket(input):
nums = [mapping[char] for char in input]
count = 0
for i in range(len(nums)):
sum = 0
for j in range(i, len(nums)):
length = j-i+1
sum += nums[j]
if sum % length == 0:
count+=1
return count
'Problem Solving' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
linked list์์ ๋ง์ง๋ง์์ n๋ฒ์งธ ๋ ธ๋ ์ฐพ๊ธฐ (0) | 2025.04.23 |
---|---|
Leetcode 143. Reorder List (0) | 2025.02.17 |