-
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
Showing
4 changed files
with
59 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 @@ | ||
add_catch(test_uncommon_words_from_two_sentences test.cpp) |
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,26 @@ | ||
#pragma once | ||
|
||
#include <sstream> | ||
#include <string> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
class Solution { | ||
public: | ||
static std::vector<std::string> uncommonFromSentences(std::string s1, | ||
std::string s2) { | ||
std::stringstream stream(s1 + " " + s2); | ||
std::unordered_map<std::string, int> counter; | ||
for (std::string word; stream >> word;) { | ||
++counter[word]; | ||
} | ||
|
||
std::vector<std::string> uncommon; | ||
for (const auto &[word, frequency] : counter) { | ||
if (frequency == 1) { | ||
uncommon.push_back(word); | ||
} | ||
} | ||
return uncommon; | ||
} | ||
}; |
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,31 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
std::string s1; | ||
std::string s2; | ||
std::vector<std::string> expected; | ||
}; | ||
|
||
std::vector<TestCase> test_cases{ | ||
{ | ||
.s1 = "this apple is sweet", | ||
.s2 = "this apple is sour", | ||
.expected{"sweet", "sour"}, | ||
}, | ||
{ | ||
.s1 = "apple apple", | ||
.s2 = "banana", | ||
.expected{"banana"}, | ||
}, | ||
}; | ||
|
||
for (auto [s1, s2, expected] : test_cases) { | ||
auto actual = Solution::uncommonFromSentences(s1, s2); | ||
std::sort(actual.begin(), actual.end()); | ||
std::sort(expected.begin(), expected.end()); | ||
REQUIRE(expected == actual); | ||
} | ||
} |