Skip to content

Instantly share code, notes, and snippets.

@DragonOsman
Last active February 21, 2022 12:36
Show Gist options
  • Save DragonOsman/cd1c08495f8d7b3511e174c90c0b1200 to your computer and use it in GitHub Desktop.
Save DragonOsman/cd1c08495f8d7b3511e174c90c0b1200 to your computer and use it in GitHub Desktop.
#include <vector>
#include <cctype>
std::vector<std::string> tokenize(const std::string& str, const char delimiter);
bool is_number(const std::string& str);
class Solution {
public:
bool areNumbersAscending(string s) {
std::vector<std::string> tokens{tokenize(s, ' ')};
bool is_larger{};
for (std::size_t i{}, n{tokens.size()}; i < n; ++i)
{
if ((i + 1) < n)
{
if (is_number(tokens[i]) && is_number(tokens[i + 1]))
{
if (tokens[i + 1] > tokens[i])
{
is_larger = true;
}
else if (tokens[i + 1] <= tokens[i])
{
is_larger = false;
break;
}
}
}
}
return is_larger;
}
};
std::vector<std::string> tokenize(const std::string& str, const char delimiter)
{
std::vector<std::string> tokens;
std::string substr;
for (std::size_t i{}, n{str.length()}; i < n; ++i)
{
if (i != n - 1)
{
if (str[i] != delimiter)
{
substr += str[i];
}
else
{
if (!substr.empty())
{
tokens.push_back(substr);
substr = "";
}
}
}
else if (str[i] == str[n - 1])
{
if (str[i] != delimiter)
{
substr += str[i];
}
if (!substr.empty())
{
tokens.push_back(substr);
}
}
}
return tokens;
}
bool is_number(const std::string& str)
{
bool flag{};
if (str.length() == 1)
{
return std::isdigit(str[0]);
}
else if (str.length() > 1)
{
for (const char ch : str)
{
if (std::isdigit(ch))
{
flag = true;
}
else
{
flag = false;
}
}
}
return flag;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment