• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How to check if characters forming string are different length

Resolved: How to check if characters forming string are different length

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

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& a, const std::pair& b) const
{
return less_comparator(std::minmax(a.first, a.second),
std::minmax(b.first, b.second));
}

std::less> less_comparator;
};

int main()
{
string word;
string notDupes = “”;
vector amount;
vector> para;
std::set, custom_comparator> unique;
vector items;
vector toIterate;
string result;

while(cin>>word) {
if(word.length() > 100) {
return 0;
}
for( int x=0;x

Answer:

You can reduce the whole thing (if I understood the question correctly) to just a few rather simple steps:
  1. Sort the input string, so duplicate characters end up next to each other.
  2. produce a list (or vector) of the occurrence counts of each distinct character.
  3. test, if the list (or vector) of those occurrence counts contains duplicates.
  4. If duplicates – output “NO” else output “YES”.

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
#include

struct Accumulator {
char current;
size_t count;
std::vector occurrences;
Accumulator(char c, size_t count, std::vector occs)
: current{c}
, count{count}
, occurrences{occs}
{
}
};

int main (int argc, const char* argv[]) {
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 deduped;
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]

And it is easily ported to other languages, offering about the same facilities. Here, for example the less verbose implementation in Common Lisp:

(defun unique-occurrence-counts-of-chars
(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]

If you have better answer, please add a comment about this, thank you!

c++
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Input Focus between two React Components

02/04/2023

Resolved: linq2db throws exception when filtering by nested collection

02/04/2023

Resolved: Table data is coming as empty in React

02/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.