-3

I am trying to write a program that checks if characters of created word are in different length. for example word:

PAABBBMMMM

it should print Yes, because P was printed 1 time, A was printed 2 times, B was printed 3 Times, m was printed 4 times.

If the word was for e.g PAABB it should print no, because AA and BB is same length.

What I am doing wrong?

#include <iostream>
using namespace std;

 
bool checkChars(string s)
{
    int n = s.length();
    for (int i = 1; i < n; i++)
        if (s[i] != s[0])
            return false;
 
    return true;
}
 
// Driver code
int main()
{
    string s = "PAABBBMMMM";
    if (checkChars(s))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}
modestMan
  • 1
  • 2
  • So, what is your question? – spectras Jun 13 '21 at 09:17
  • The program doesn't work correctly. – modestMan Jun 13 '21 at 09:18
  • So would `ABAB` print yes or no? Or, in general, do the characters needs to be adjacent (like `AABB`) to be counted as words? – silverfox Jun 13 '21 at 09:24
  • How do you expect your code to work? It seems to just turn true if all characters are the same as the first one? – Alan Birtles Jun 13 '21 at 09:24
  • @silverfox should print no, because A is 2 twice nad B is twice, doesnt matter if its `ABAB` or` AABB` – modestMan Jun 13 '21 at 09:25
  • @silverfox if it was `ABB` it should print yes, could also look like `BAB` – modestMan Jun 13 '21 at 09:26
  • Take a look at your code. Try to explain it to someone else. Your friend, your mom, your plant. Then you might start to wonder why you always compare with `s[0]`. It looks like you actually want to _count_ characters. You should first come up with a basic idea. How would you do it with pen and paper? – Lukas-T Jun 13 '21 at 09:29

1 Answers1

0

With the condition if (s[i] != s[0]), you're just checking if each character is equal to the first character, which makes no sense.

You can use a std::map to count the frequency of each character, then use std::set to check the uniqueness of each frequency:

#include <iostream>
#include <string>
#include <map>
#include <set>

bool checkChars(std::string s)
{
    int n = s.length(); std::map<int, char> freq;
    for (int i = 0; i < n; i++)
        freq[s[i]]++;

    std::set<int> Unique;
    for (auto it = freq.begin(); it!=freq.end(); it++)
        Unique.insert(it->second);

    if (Unique.size() != freq.size()) {return false;}
    return true;
}

// Driver code
int main()
{
    std::string s = "PAABBBMMMM";
    if (checkChars(s))
        std::cout << "Yes";
    else
        std::cout << "No";

    return 0;
}

Result: Yes

Other example:

  • "AABB" -> No
  • "MABAAB" -> Yes

Also see Why is "using namespace std;" considered bad practice?

silverfox
  • 1,568
  • 10
  • 27
  • Thank you, could you also tell me how can I allow for input only latin alphabet with capital letters? – modestMan Jun 13 '21 at 09:34
  • @modestMan You can loop through every character in the input string and check it with `isupper()` : https://en.cppreference.com/w/cpp/string/byte/isupper – silverfox Jun 13 '21 at 09:37
  • Also, I am getting compilation error ` error: ‘it’ does not name a type` `file.cpp:14:34: error: expected ‘;’ before ‘it’` – modestMan Jun 13 '21 at 09:40
  • Well then, from this compilation : https://ideone.com/Qe41R0, the code works fine, so maybe there's another problem. – silverfox Jun 13 '21 at 09:44
  • is it possible to change this loop to work in c++ 98? – modestMan Jun 13 '21 at 09:47
  • @modestMan If you want a program that works in a specific version, you should have mentioned it in the question. My program is only compilable with C++11 and above. And why don't you use a more recent version of C++? – silverfox Jun 13 '21 at 09:49