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

2025. 1. 2. 11:19·알고리즘/LeetCode

 

난이도: Easy

 

문제 설명


Given a string s of zeros and ones, return the maximum score after splitting the string into two non-empty substrings (i.e. left substring and right substring).

 

The score after splitting a string is the number of zeros in the left substring plus the number of ones in the right substring.

 

문제 예제


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

 

제한 사항


  • 2 <= s.length <= 500
  • The string s consists of characters '0' and '1' only.

 

✏️ Solution(솔루션)


class Solution:
    def maxScore(self, s: str) -> int:
        answer = 0

        dic_left = {'0':0, '1':0}
        dic_right = {'0':0, '1':0}
        
        for num in s:
            dic_right[num] += 1
        
        for i in range(len(s)-1):
            dic_right[s[i]] -= 1
            dic_left[s[i]] += 1
            answer = max(answer, dic_right['1'] + dic_left['0'])

        return answer

 

먼저 '0'과 '1'을 key로 갖는 딕셔너리 2개를 만들었다.

 

하나는 left를 가리키는 딕셔너리이고 하나는 right를 가리키는 딕셔너리이다.

 

초기에 left 딕셔너리가 갖는 key들의 value를 모두 0으로 설정하고 right는 s가 가지고 있는 '0'과 '1'의 개수를 value로 설정한다.

 

그리고 s에 각 요소마다 dic_left[s[i]]는 +1을 하고 dic_right[s[i]]는 -1을 해준다.

다음으로 max함수를 통해(answer = max(answer, dic_right['1'] + dic_left['0'])) 제일 큰 값을 answer에 저장하고, 정답으로 return 하였다.

 


문제: 1422. Maximum Score After Splitting a String

깃허브: github

 

algorithm_practice/LeetCode/1537-maximum-score-after-splitting-a-string at main · laewonJeong/algorithm_practice

하루 한 문제 챌린지. Contribute to laewonJeong/algorithm_practice development by creating an account on GitHub.

github.com

 

'알고리즘 > LeetCode' 카테고리의 다른 글

[LeetCode] 2270. Number of Ways to Split Array (Python)  (1) 2025.01.03
[LeetCode] 2559. Count Vowel Strings in Ranges (Python)  (1) 2025.01.02
[LeetCode] 322. Coin Change (Python)  (0) 2024.12.31
[LeetCode] 983. Minimum Cost For Tickets (Python)  (0) 2024.12.31
[LeetCode] 2466. Count Ways To Build Good Strings (Python)  (0) 2024.12.30
'알고리즘/LeetCode' 카테고리의 다른 글
  • [LeetCode] 2270. Number of Ways to Split Array (Python)
  • [LeetCode] 2559. Count Vowel Strings in Ranges (Python)
  • [LeetCode] 322. Coin Change (Python)
  • [LeetCode] 983. Minimum Cost For Tickets (Python)
Laewon Jeong
Laewon Jeong
  • Laewon Jeong
    래원
    Laewon Jeong
    글쓰기 | 관리
  • GitHub

    • github.com/laewonJeong
  • 전체
    오늘
    어제
    • 분류 전체보기 (172)
      • Docker 및 Kubernetes (11)
        • Docker (5)
        • Kubernetes (6)
      • Data Engineering (18)
        • Hadoop (5)
        • Spark (5)
        • Kafka (5)
        • Airflow (3)
      • CI|CD (3)
      • 알고리즘 (136)
        • 알고리즘 (2)
        • LeetCode (118)
        • 프로그래머스 (11)
        • BOJ (1)
        • 코딩테스트 대비 (4)
      • 서버 (2)
        • 미니 서버 (2)
      • 잡담 (1)
  • 태그

    그래프
    Kubernetes
    분산
    Python
    오블완
    BFS
    문자열
    leetcode
    누적합
    Apache Spark
    우선순위큐
    리트코드
    백트래킹
    분산처리
    파이썬
    티스토리챌린지
    programmers
    도커
    프로그래머스
    아파치 하둡
    dfs
    heapq
    이진탐색
    쿠버네티스
    코딩테스트
    docker
    DP
    아파치 스파크
    알고리즘
    String
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Laewon Jeong
[LeetCode] 1422. Maximum Score After Splitting a String (Python)
상단으로

티스토리툴바