Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nikhedonia/107d4a68c79e2e24ad2ca10ff296deb5 to your computer and use it in GitHub Desktop.
Save nikhedonia/107d4a68c79e2e24ad2ca10ff296deb5 to your computer and use it in GitHub Desktop.
Gist created by https://fiddle.jyt.io
#include<iostream>
#include<vector>
#include<string>
#include<set>
auto numberOfUniqueChars(std::string const& str) {
std::set<char> s;
for(auto c : str) {
s.insert(c);
}
return s.size();
}
auto getStringWithTheMostUniqueChars(std::vector<std::string> const& strings) {
size_t maxCount = 0;
std::string result;
for(auto s : strings) {
const auto currentCount = numberOfUniqueChars(s);
if( currentCount > maxCount ) {
result = s;
maxCount = currentCount;
}
}
return result;
}
auto test0(){
return getStringWithTheMostUniqueChars(
{}
) == "";
}
auto test1(){
return getStringWithTheMostUniqueChars(
{"asd", "asdf", "aaa" }
) == "asdf";
}
int main() {
std::vector<std::string> strings;
std::cout << "enter your strings; enter $ to end" <<std::endl;
while (1) {
std::string line;
std::getline(std::cin, line);
if(line=="$") break;
std::cout << "input: "
<< '\"' << line <<'\"'
<< " unique chars: "
<< numberOfUniqueChars(line)
<< std::endl;
strings.push_back(line);
}
std::cout << "string with most unique chars: "
<< getStringWithTheMostUniqueChars(strings)
<< std::endl;
return 0;
}
/*
press play and call main or testX
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment