[Programmers] Lv2. ๋ฌธ์์ด ์์ถ | C++
2023. 6. 30. 00:15ใCoding Test/Programmers
๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐จ๐ปํ์ด ๊ณผ์
๋ฌธ์ ์์ ์ฃผ์ด์ง ๋ด์ฉ๋๋ก ๊ตฌํํ๋ฉด ์๊ฐ ์ด๊ณผ๋์ง ์์๊น ํ๋๋ฐ, ํต๊ณผ๋์๋ค์. ๋ฌธ์ ๊ฐ ๊ทธ๋ฆฌ ์ด๋ ต์ง ์์์ต๋๋ค. ๋ธ๋ฃจํธ ํฌ์ค๋ก ํ์ด๋ ๋ ๊ฒ ๊ฐ์ต๋๋ค.
โ๏ธ์์ค ์ฝ๋ ๋ฐ ๊ฒฐ๊ณผ
#include <string>
using namespace std;
int solution(string s) {
int answer = 10000;
int extractNum = 1;
while (extractNum <= s.length()) {
string preToken = "";
string compressedString = "";
int repeatCount = 1;
for (int i = 0; i < s.length(); i += extractNum) {
string token = s.substr(i, extractNum);
if (preToken.empty()) {
preToken = token;
continue;
}
if (preToken != token) {
string strRepeatCount = repeatCount == 1 ? "" : to_string(repeatCount);
compressedString += strRepeatCount;
compressedString += preToken;
// ์ต์๊ฐ๋ณด๋ค ํฐ ๊ฒฝ์ฐ๋ ๋ณผ ํ์ ์์ผ๋ฏ๋ก ๋ค์ ๋ฃจํ๋ก
if (answer < compressedString.length())
continue;
preToken = token;
repeatCount = 1;
continue;
}
repeatCount++;
}
string strRepeatCount = repeatCount == 1 ? "" : to_string(repeatCount);
compressedString += strRepeatCount;
compressedString += preToken;
answer = min(answer, (int)compressedString.length());
extractNum++;
}
return answer;
}
728x90
๋ฐ์ํ
'Coding Test > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] Lv2. ๋ํ์ค ๊ฒ์ | C++ (0) | 2023.07.01 |
---|---|
[Programmers] Lv2. ํ ์ด๋ธ ํด์ ํจ์ | C++ (0) | 2023.07.01 |
[Programmers] Lv2. ํ๋ ฌ ํ ๋๋ฆฌ ํ์ ํ๊ธฐ | C++ (0) | 2023.06.28 |
[Programmers] Lv2. ์ฐ์๋ ๋ถ๋ถ ์์ด์ ํฉ | C++ (0) | 2023.06.27 |
[Programmers] Lv2. ํฐ ์ ๋ง๋ค๊ธฐ | C++ (0) | 2023.06.27 |