-
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.
Added Remove Colored Pieces if Both Neighbors are the Same Color
- Loading branch information
1 parent
d600601
commit 747622f
Showing
4 changed files
with
74 additions
and
1 deletion.
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
24 changes: 24 additions & 0 deletions
24
Medium/RemoveColoredPiecesIfBothNeighborsAreTheSameColor/include/solution.hpp
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,24 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <gtest/gtest.h> | ||
|
||
class Solution { | ||
public: | ||
static auto winner_of_game(const std::string &colors) -> bool { | ||
int32_t bob = 0; | ||
int32_t alice = 0; | ||
|
||
for (size_t i = 1; i < colors.size() - 1; ++i) { | ||
if (colors[i - 1] == colors[i] | ||
&& colors[i] == colors[i + 1]) { | ||
if(colors[i] == 'A') ++alice; | ||
if(colors[i] == 'B') ++bob; | ||
} | ||
} | ||
|
||
return (alice >= (bob + 1)); | ||
} | ||
}; |
38 changes: 38 additions & 0 deletions
38
Medium/RemoveColoredPiecesIfBothNeighborsAreTheSameColor/tests/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,38 @@ | ||
#include "../include/solution.hpp" | ||
|
||
class RemoveColoredPiecesIfBothNeighborsAreTheSameColor : public ::testing::Test { | ||
protected: | ||
~RemoveColoredPiecesIfBothNeighborsAreTheSameColor() override = default; | ||
}; | ||
|
||
TEST_F(RemoveColoredPiecesIfBothNeighborsAreTheSameColor, FirstTest) { | ||
std::string colors = "AAABABB"; | ||
|
||
bool result = Solution::winner_of_game(colors); | ||
bool expected = true; | ||
|
||
ASSERT_EQ(result, expected); | ||
} | ||
|
||
TEST_F(RemoveColoredPiecesIfBothNeighborsAreTheSameColor, SecondTest) { | ||
std::string colors = "AA"; | ||
|
||
bool result = Solution::winner_of_game(colors); | ||
bool expected = false; | ||
|
||
ASSERT_EQ(result, expected); | ||
} | ||
|
||
TEST_F(RemoveColoredPiecesIfBothNeighborsAreTheSameColor, ThirdTest) { | ||
std::string colors = "ABBBBBBBAAA"; | ||
|
||
bool result = Solution::winner_of_game(colors); | ||
bool expected = false; | ||
|
||
ASSERT_EQ(result, expected); | ||
} | ||
|
||
auto main(int argc, char **argv) -> int { | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |