-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1829d11
commit dde2cdc
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#pragma once | ||
|
||
#include <stack> | ||
#include <vector> | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <gtest/gtest.h> | ||
|
||
class Solution { | ||
public: | ||
static auto decode_string(const std::string &word) -> std::string { | ||
std::stack<std::string> substrings; | ||
std::stack<size_t> multipliers; | ||
size_t multiplier = 0; | ||
std::string current_sub; | ||
|
||
for (const char &ch : word) { | ||
if (isdigit(ch)) { | ||
multiplier = multiplier * 10 + (ch - '0'); | ||
} else if (ch == '[') { | ||
multipliers.push(multiplier); | ||
substrings.push(current_sub); | ||
multiplier = 0; | ||
current_sub.clear(); | ||
} else if (ch == ']') { | ||
size_t repeat_count = multipliers.top(); | ||
multipliers.pop(); | ||
std::string previous_sub = substrings.top(); | ||
substrings.pop(); | ||
for (size_t i = 0; i < repeat_count; ++i) { | ||
previous_sub += current_sub; | ||
} | ||
current_sub = previous_sub; | ||
} else current_sub += ch; | ||
} | ||
|
||
return current_sub; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "../include/solution.hpp" | ||
|
||
class DecodeString : public ::testing::Test { | ||
protected: | ||
~DecodeString() override = default; | ||
}; | ||
|
||
TEST_F(DecodeString, FirstTest) { | ||
std::string word = "3[a]2[bc]"; | ||
|
||
std::string result = Solution::decode_string(word); | ||
|
||
std::string expected = "aaabcbc"; | ||
ASSERT_EQ(result, expected); | ||
} | ||
|
||
TEST_F(DecodeString, SecondTest) { | ||
std::string word = "3[a2[c]]"; | ||
|
||
std::string result = Solution::decode_string(word); | ||
|
||
std::string expected = "accaccacc"; | ||
ASSERT_EQ(result, expected); | ||
} | ||
|
||
TEST_F(DecodeString, ThirdTest) { | ||
std::string word = "2[abc]3[cd]ef"; | ||
|
||
std::string result = Solution::decode_string(word); | ||
|
||
std::string expected = "abcabccdcdcdef"; | ||
ASSERT_EQ(result, expected); | ||
} | ||
|
||
auto main(int argc, char **argv) -> int { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |