반응형
Define a word as a sequence of consecutive English letters. Find the longest word from the given string. Example For text = "Ready, steady, go!", the output should be longestWord(text) = "steady". |
code>>
std::string longestWord(std::string text) {
std::regex reg("\\w+([a-zA-Z])");
std::smatch m;
std::string str="";
std::string temp = text;
while (std::regex_search(temp, m, reg))
{
for (auto t : m)
{
if (str.length() < t.length())
str = t;
}
temp = m.suffix();
}
return str;
}
반응형
'알고리즘 > codefights' 카테고리의 다른 글
54>sumUpNumbers (0) | 2018.03.14 |
---|---|
53>validTime (0) | 2018.02.10 |
51>deleteDigit (0) | 2018.02.01 |
50>chessKnight (0) | 2017.11.04 |
49>lineEncoding (0) | 2017.11.04 |