Problem:
Given a string, return its encoding defined as follows:
- First, the string is divided into the least possible number of disjoint substrings consisting of identical characters
- for example, "aabbbc" is divided into ["aa", "bbb", "c"]
- Next, each substring with length greater than one is replaced with a concatenation of its length and the repeating character
- for example, substring "bbb" is replaced by "3b"
- Finally, all the new strings are concatenated together in the same order and a new string is returned.
Example
For s = "aabbbc", the output should be
lineEncoding(s) = "2a3bc".
-Summary-
1. for loop 을 돌며, 마지막 index 전까지 아래의 check logic으로 res variable에 넣어준다.
- 만약 현재 character 가 다음 character와 같다면 count 수 증가
- 만약 현재 character 가 다음 character와 같지 않고 count 가 2 이상이라면, res 에 여태 count한 숫자와 character를 넣어준다. count 는 1로 다시 초기화.
- 만약 현재 character 가 다음 character 와 같지 않지만 count 가 2보다 작다면, 그냥 단어만 추가해주고 count 도 1로 다시 초기화.
2. 위의 과정 후 마지막 element를 체크해줘야한다.
- 만약 다음 index가 마지막 index 이고, 마지막 index가 현재 character와 같지 않다면 마지막 단어만 추가.
- 만약 다음 index가 마지막 index 이고, 마지막 index가 현재 character와 같다면, 여태까지 count 한 값 + 단어 추가.
모든 문제에 대한 저작권은 CodeSignal 회사에 있습니다. [Copyright © 2020 BrainFights Inc. All rights reserved]
'CodeSignal > Arcade' 카테고리의 다른 글
CodeSignal [51/60] deleteDigit (0) | 2020.05.28 |
---|---|
CodeSignal [50/60] chessKnight (0) | 2020.05.28 |
CodeSignal [48/60] isDigit (0) | 2020.05.27 |
CodeSignal [47/60] isMAC48Address (0) | 2020.05.27 |
CodeSignal [46/60] electionsWinners (0) | 2020.05.27 |
댓글