Skip to content

Commit e827dfa

Browse files
Merge pull request #1625 from CodingTestStudy2/한재원
[한재원] Day02
2 parents b7e48df + 371e392 commit e827dfa

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
input: string of alphabet letters
3+
output: boolean - if the string can be split into substrings with equal scores
4+
5+
- each alphabet has the score a=1... z=26
6+
7+
- make dictionary key: a-z, value: 1-26
8+
- two pointer
9+
adcb
10+
| |
11+
- while check current point of each pointer
12+
- if left > right
13+
move right pointer to the left
14+
- else
15+
move left pointer to the right
16+
- return if the score is equal
17+
- return false
18+
19+
TC: O(N), SC: O(26) - O(1)
20+
21+
'''
22+
class Solution:
23+
def scoreBalance(self, s: str) -> bool:
24+
score_dict = {}
25+
26+
for i in range(26):
27+
score_dict[chr(ord('a') + i)] = i + 1
28+
29+
total = 0
30+
31+
for ch in s:
32+
total += score_dict[ch]
33+
34+
left = 0
35+
36+
for i in range(len(s) - 1):
37+
left += score_dict[s[i]]
38+
right = total - left
39+
40+
if left == right:
41+
return True
42+
43+
return False

0 commit comments

Comments
 (0)