-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlook_and_say.h
More file actions
33 lines (29 loc) · 794 Bytes
/
Copy pathlook_and_say.h
File metadata and controls
33 lines (29 loc) · 794 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
#ifndef CPP_ALGORITHM_LOOK_AND_SAY_H
#define CPP_ALGORITHM_LOOK_AND_SAY_H
#include <string>
namespace LookAndSay
{
/**
* \brief Look and say problem.
* \param input input string
* \return result string
*/
std::string LookAndSayProblem(const std::string& input);
}
// ----------------------------------------------------------------------------
inline std::string LookAndSay::LookAndSayProblem(const std::string& input)
{
std::string result;
for (int i = 0; i < static_cast<int>(input.size()); ++i)
{
int count = 1;
while (i + 1 < static_cast<int>(input.size()) && input[i] == input[i + 1])
{
++count;
++i;
}
result += std::to_string(count) + input[i];
}
return result;
}
#endif