File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments