-
CompressmyCode/ShortestCodeChallenge 2016. 4. 13. 11:55
2000
You are given a string
s
. Compress it by replacing its consecutive equal characters with the single one.Example
For
s = "zz000!!G"
, the answer should becompress(s) = "z0!G"
.[input] string s
A string that may consists of Latin letters, digits and punctuation marks.
0 ≤ s.length ≤ 4550
.[output] string
String
s
compressed as described above.
String 관련 문제는 항상 헷갈린다.
원체 활용도도 많고 C++ 표준 라이브러리에도 제공하는 함수가 많다.
쓸 때 마다 '아, 이렇게 썼었는데 맞나?' 하면서 구글링하는게 거의 일상이다.
이 기회에 자주 쓰는 것들을 체계적으로 정리를 해봐야겠다.
짜기에 따라서 n2의 시간 복잡도까지 가질 수 있다.
- 참조
http://stackoverflow.com/questions/27663775/remove-consecutive-duplicate-values-in-a-string
http://www.cplusplus.com/reference/algorithm/unique/ (std::unique 설명)
http://www.cplusplus.com/reference/string/string/erase/ (std::erase 설명)
'myCode > ShortestCodeChallenge' 카테고리의 다른 글
DigitBuilder [Subset sum problem] (0) 2016.04.25 Last Two [Fast Exponentiation Algorithm] (0) 2016.04.21 ChessBoardShapes [Flood Fill Algorithm] (0) 2016.04.21 The Euler's totient (0) 2016.04.07 PolynomialRoot (0) 2016.04.06