Skip to content

Commit

Permalink
Keyboard Row
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Oct 13, 2023
1 parent 4f247aa commit f87cc82
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ add_task(jump-game-iii)
add_task(jump-game-iv)
add_task(k-closest-points-to-origin)
add_task(k-radius-subarray-averages)
add_task(keyboard-row)
add_task(keys-and-rooms)
add_task(kids-with-the-greatest-number-of-candies)
add_task(knight-probability-in-chessboard)
Expand Down
1 change: 1 addition & 0 deletions solutions/keyboard-row/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_keyboard_row test.cpp)
27 changes: 27 additions & 0 deletions solutions/keyboard-row/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <algorithm>
#include <cctype>
#include <set>
#include <string>
#include <vector>

class Solution {
public:
static std::vector<std::string>
findWords(const std::vector<std::string> &words) {
const std::vector<int> rows{2, 3, 3, 2, 1, 2, 2, 2, 1, 2, 2, 2, 3,
3, 1, 1, 1, 1, 2, 1, 1, 3, 1, 3, 1, 3};
std::vector<std::string> ans;
for (const auto &word : words) {
std::set<int> set;
std::transform(
word.begin(), word.end(), std::inserter(set, set.begin()),
[&rows](unsigned char c) { return rows[std::tolower(c) - 'a']; });
if (set.size() == 1) {
ans.push_back(word);
}
}
return ans;
}
};
30 changes: 30 additions & 0 deletions solutions/keyboard-row/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <catch.hpp>

#include <solution.hpp>

TEST_CASE("Simple") {
struct TestCase {
std::vector<std::string> words;
std::vector<std::string> expected;
};

std::vector<TestCase> test_cases{
{
.words{"Hello", "Alaska", "Dad", "Peace"},
.expected{"Alaska", "Dad"},
},
{
.words{"omk"},
.expected{},
},
{
.words{"adsdf", "sfd"},
.expected{"adsdf", "sfd"},
},
};

for (const auto &[words, expected] : test_cases) {
const auto actual = Solution::findWords(words);
REQUIRE(expected == actual);
}
}

0 comments on commit f87cc82

Please sign in to comment.