-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHappyLadybug.cpp
More file actions
41 lines (39 loc) · 922 Bytes
/
HappyLadybug.cpp
File metadata and controls
41 lines (39 loc) · 922 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
https://www.hackerrank.com/challenges/happy-ladybugs/problem
*/
string happyLadybugs(string b) {
// empty string case
if(b == "")
return "NO";
int underscoreCounter = 0;
int map[26] = {0};
// increment cells to see if all cells can be happy
bool notInOrder = false;
for(int i = 0; i < b.length(); i++)
{
if(b[i] == '_')
underscoreCounter++;
else
map[b[i] - 'A']++;
}
if(b.length() > 2)
{
for(int i = 0; i < b.length() - 2; i++)
{
if(b[i] != b[i + 1] && b[i + 1] != b[i + 2])
{
notInOrder = true;
}
}
}
// no empty spots
if(underscoreCounter == 0 && notInOrder)
return "NO";
// check if there are no happy bugs, at least on is enough
for(int i = 0; i < 26; i++)
{
if(map[i] == 1)
return "NO";
}
return "YES";
}