Skip to content

Commit

Permalink
First Letter to Appear Twice
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Oct 17, 2023
1 parent af3397a commit 9e7e276
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ add_task(find-the-student-that-will-replace-the-chalk)
add_task(find-the-town-judge)
add_task(find-the-winner-of-the-circular-game)
add_task(first-bad-version)
add_task(first-letter-to-appear-twice)
add_task(first-missing-positive)
add_task(first-unique-character-in-a-string)
add_task(fizz-buzz)
Expand Down
1 change: 1 addition & 0 deletions solutions/first-letter-to-appear-twice/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_first_letter_to_appear_twice test.cpp)
17 changes: 17 additions & 0 deletions solutions/first-letter-to-appear-twice/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <string>
#include <vector>

class Solution {
public:
static char repeatedCharacter(std::string s) {
std::vector<int> counter(128);
for (auto c : s) {
if (counter[c]++) {
return c;
}
}
throw;
}
};
26 changes: 26 additions & 0 deletions solutions/first-letter-to-appear-twice/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <catch.hpp>

#include <solution.hpp>

TEST_CASE("Simple") {
struct TestCase {
std::string s;
char expected;
};

std::vector<TestCase> test_cases{
{
.s = "abccbaacz",
.expected = 'c',
},
{
.s = "abcdd",
.expected = 'd',
},
};

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

0 comments on commit 9e7e276

Please sign in to comment.