Skip to content

Commit

Permalink
Added Reverse Words in a String III
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace-Krypton committed Oct 1, 2023
1 parent 9f6a85a commit 2a35c97
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1173,4 +1173,15 @@ add_executable(EqualRowAndColumnPairs
target_link_libraries(
EqualRowAndColumnPairs
GTest::gtest_main
)

# Reverse Words in a String III
add_executable(ReverseWordsInAStringIII
Easy/ReverseWordsInAStringIII/include/solution.hpp
Easy/ReverseWordsInAStringIII/tests/test.cpp
)

target_link_libraries(
ReverseWordsInAStringIII
GTest::gtest_main
)
23 changes: 23 additions & 0 deletions Easy/ReverseWordsInAStringIII/include/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <vector>
#include <iostream>
#include <algorithm>
#include <gtest/gtest.h>

class Solution {
public:
static auto reverse_words(std::string &word) -> std::string {
std::stringstream ss(word);
std::string result;

while (ss >> word) {
std::string reversed_word(word.rbegin(), word.rend());
result += reversed_word + " ";
}

if (!result.empty()) result.pop_back();

return result;
}
};
29 changes: 29 additions & 0 deletions Easy/ReverseWordsInAStringIII/tests/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "../include/solution.hpp"

class ReverseWordsInAStringIII : public ::testing::Test {
protected:
~ReverseWordsInAStringIII() override = default;
};

TEST_F(ReverseWordsInAStringIII, FirstTest) {
std::string word = "Let's take LeetCode contest";

std::string result = Solution::reverse_words(word);
std::string expected = "s'teL ekat edoCteeL tsetnoc";

ASSERT_EQ(result, expected);
}

TEST_F(ReverseWordsInAStringIII, SecondTest) {
std::string word = "God Ding";

std::string result = Solution::reverse_words(word);
std::string expected = "doG gniD";

ASSERT_EQ(result, expected);
}

auto main(int argc, char **argv) -> int {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit 2a35c97

Please sign in to comment.