-
프로그래머스 / 문자열 나누기코딩 테스트/Level 1 2022. 12. 2. 20:25반응형
https://school.programmers.co.kr/learn/courses/30/lessons/140108
파이썬
def solution(s): first = s[0] new_index = slices = same = no_same = 0 for index, each in enumerate(s): new_index += 1 if each == first: same += 1 else: no_same += 1 if same == no_same: slices += 1 new_index = same = no_same = 0 if index + 1 < len(s): first = s[index + 1] return slices + (1 if new_index else 0)
def solution(s): slices = same = no_same = 0 first = '' for each in s: if same == no_same: slices += 1 first = each if each == first: same += 1 else: no_same += 1 return slices
코틀린
class Solution { fun solution(s: String): Int { var (answer, same, different) = Triple(0, 0, 0) var before = ' ' for (each in s) { if (same == different) { answer++ before = each } if (each == before) same++ else different++ } return answer } }
반응형