Question:
I want to create a function that takes in a string as a parameter and checks if the number of occurrences of the individual letters are different."OBDO"
should display NO
, because O
occurs twice, but B
and D
occur once."AABBB"
should display YES
, because A
occurs twice, and B
occurs three times.My code seems to work, but my code auto checker wont accept it, out of 4 tests it only passes once. I believe this can be done way better and shorter.
Can anyone advise?
#include
#include
#include
#include
#include
using namespace std;
struct custom_comparator {
bool operator()(const std::pair
{
return less_comparator(std::minmax(a.first, a.second),
std::minmax(b.first, b.second));
}
std::less
};
int main()
{
string word;
string notDupes = “”;
vector
vector
std::set
vector
vector
string result;
while(cin>>word) { #include struct Accumulator { int main (int argc, const char* argv[]) { (defun unique-occurrence-counts-of-chars If you have better answer, please add a comment about this, thank you!
if(word.length() > 100) {
return 0;
}
for( int x=0;xAnswer:
You can reduce the whole thing (if I understood the question correctly) to just a few rather simple steps:
step 1 is easily done, using std::sort
.
step 2 is not quite as obvious- but a std::accumulate
(similar to reduce
and also often called fold
) can help with that, if we maintain the current counting and listing state in the accumulator of the operation, while iterating over the sorted strings characters.
step 3, we can do by using a std::set<size_t>
, inserting the resulting list from step 2 into it and then compare the size of the set with the size of the list.
In C++, this can look like this:
#include
#include
#include
#include
#include
#include
char current;
size_t count;
std::vector
Accumulator(char c, size_t count, std::vector
: current{c}
, count{count}
, occurrences{occs}
{
}
};
if (argc >= 1) {
std::string s = argv[1];
auto sorted_s = s;
std::sort(sorted_s.begin(),sorted_s.end(),std::less
Accumulator x =
std::accumulate(sorted_s.cbegin() + 1,
sorted_s.cend(),
Accumulator(*sorted_s.cbegin(),1,{}),
[](Accumulator acc, char c) {
if (c == acc.current) {
return Accumulator(c,
acc.count + 1,
acc.occurrences);
} else {
auto acc1 = Accumulator(c,1,acc.occurrences);
acc1.occurrences.push_back(acc.count);
return acc1;
}
});
x.occurrences.push_back(x.count);
std::set
for (auto& k : x.occurrences) {
deduped.insert(k);
}
if (deduped.size() == x.occurrences.size()) {
std::cout << "Yes" << std::endl;
} else {
std::cout << "No" << std::endl;
}
} else {
std::cout << "no input." << std::endl;
}
return 0;
}
[/code]
(s)
(let ((sorted-s (sort (copy-seq s) #’char<)))
(let ((x
(reduce #'(lambda (acc c)
(if (char= (first (first acc)) c)
(list
(list c (+ (second (first acc)) 1))
(second acc))
(list
(list c 1)
(cons (second (first acc))
(second acc)))))
(subseq sorted-s 1)
:initial-value (list (list (aref sorted-s 0) 1)
'()))))
(let ((y (cons (second (first x)) (second x))))
(let ((deduped-y (remove-duplicates y)))
(if (= (length y) (length deduped-y))
"yes"
"no"))))))
[/code]