Skip to content

Commit 3f989a5

Browse files
authored
Add solution for Equal Score Substrings problem
Implement two-pointer approach to check if score balances.
1 parent f154358 commit 3f989a5

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#
2+
3+
'''
4+
1. 아이디어 :
5+
투포인터를 사용한다.
6+
두 포인터가 겹칠때까지 움직이며 값을 계산한다.
7+
8+
2. 시간복잡도 :
9+
O(n)
10+
11+
3. 자료구조/알고리즘 :
12+
투포인터
13+
14+
'''
15+
16+
class Solution:
17+
def scoreBalance(self, s: str) -> bool:
18+
n = len(s)
19+
left, right = 0, n-1
20+
l_val = 0
21+
r_val = 0
22+
23+
while left <= right:
24+
if l_val <= r_val:
25+
l_val+=ord(s[left])-96
26+
left+=1
27+
else:
28+
r_val+=ord(s[right])-96
29+
right-=1
30+
return l_val == r_val
31+
32+

0 commit comments

Comments
 (0)