def solution(s):
word_lst = list(s.split(" "))
answer_lst = []
for word in word_lst:
new_word = []
# 한 단어를 변환
for idx in range(len(word)):
if idx % 2 == 0: # 홀수
new_word.append(word[idx].upper())
else: # 짝수
new_word.append(word[idx].lower())
answer_lst.append(''.join(new_word))
return ' '.join(answer_lst)
공간 복잡도가 높은 풀이였다.
공간 복잡도를 낮추기 위해서 원래 문자열에 바로 접근하여 최적화를 진행할 수 있다.
새로운 변수를 선언하는 기준은 원본 데이터가 아닌 아예 새로운 데이터를 저장해야 할 때이다.
즉, 어떤 정보를 기억해야 하는 지에 따라서 새로운 변수를 선언할 지 말 지 판단할 수 있다.
def solution(s):
idx = 0
for i in range(len(s)):
if s[i] == ' ':
idx = 0
continue
s[i] = s[i].upper() if cnt % 2 == 0 else s[i].lower()
idx += 1
return ''.join(s)