상세 컨텐츠

본문 제목

[LeetCode][Python] 1422. Maximum Score After Splitting a String

공부

by 근성 2023. 12. 22. 16:19

본문

[구현]

문제 이해

0과 1로 구성된 문자열을 input으로 제공하고, 순차적으로 문자열의 index를 증가시킨다.

index를 기준으로 0의 갯수를 counting하는 left 문자열, 1의 갯수를 counting하는 right 문자열있다.

counting갯수가 최대일때의 값을 구하라.

 

 

Example 1:

Input: s = "011101"
Output: 5 
Explanation: 
All possible ways of splitting s into two non-empty substrings are:
left = "0" and right = "11101", score = 1 + 4 = 5 
left = "01" and right = "1101", score = 1 + 3 = 4 
left = "011" and right = "101", score = 1 + 2 = 3 
left = "0111" and right = "01", score = 1 + 1 = 2 
left = "01110" and right = "1", score = 2 + 1 = 3

Example 2:

Input: s = "00111"
Output: 5
Explanation: When left = "00" and right = "111", we get the maximum score = 2 + 3 = 5

Example 3:

Input: s = "1111"
Output: 3

 

 

 

class Solution:
    def maxScore(self, s: str) -> int:
        answer = 0
        for i in range(len(s)-1):
            answer = max(s[:i+1].count('0') + s[i+1:].count('1'), answer)
        return answer

 

관련글 더보기

댓글 영역