3330 solved
This commit is contained in:
parent
b18a1fcafc
commit
7a791e5c46
59
3330/3330.cpp
Normal file
59
3330/3330.cpp
Normal file
@ -0,0 +1,59 @@
|
||||
#include <string>
|
||||
|
||||
namespace {
|
||||
class Solution {
|
||||
public:
|
||||
int possibleStringCount(std::string word) {
|
||||
const size_t word_length = word.length();
|
||||
if (word_length == 1)
|
||||
return 1;
|
||||
|
||||
int result = 1;
|
||||
|
||||
int group_size = 1;
|
||||
int groups_count = 1;
|
||||
char prev_char = word[0];
|
||||
|
||||
for (size_t i = 1; i < word_length; ++i) {
|
||||
char curr_char = word[i];
|
||||
|
||||
if (curr_char != prev_char) {
|
||||
if (group_size > 1) {
|
||||
result += group_size - 1;
|
||||
}
|
||||
|
||||
groups_count += 1;
|
||||
group_size = 1;
|
||||
} else {
|
||||
group_size += 1;
|
||||
}
|
||||
|
||||
prev_char = curr_char;
|
||||
}
|
||||
|
||||
if (group_size > 1) {
|
||||
result += group_size - 1;
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(P3330, 1) {
|
||||
Solution s;
|
||||
EXPECT_EQ(s.possibleStringCount("abbcccc"), 5);
|
||||
}
|
||||
|
||||
TEST(P3330, 2) {
|
||||
Solution s;
|
||||
EXPECT_EQ(s.possibleStringCount("abcd"), 1);
|
||||
}
|
||||
|
||||
TEST(P3330, 3) {
|
||||
Solution s;
|
||||
EXPECT_EQ(s.possibleStringCount("aaaa"), 4);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user