Check if a string has all unique characters This is a question from the book "Cracking the Coding Interview".


  Implement an algorithm to determine if a string has all unique
  characters What if you can not use additional data structures?


I can use std::map but in interview they check logic. Suggest me some improvement for optimization.

#include &lt;iostream&gt;
#include &lt;string&gt;

class Strings
{
   static const int SIZE = 26;
   char alphabetL[SIZE] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
                            'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
                            'U', 'V', 'W', 'X', 'Y', 'Z'};

   char alphabetS[SIZE] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                            'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
                            'u', 'v', 'w', 'x', 'y', 'z'};

  public:
    Strings()  = default;
    ~Strings() = default;

    bool isAllUnique(std::string&amp;);

  private:
    std::string toLower(std::string&amp;);
};

std::string Strings::toLower(std::string&amp; str)
{
    for (int i = 0; i &lt; str.length(); i++)
    {
        for (int j = 0; j &lt; SIZE; j++)
        {
            if (str[i] == alphabetL[j])
            {
                str[i] = alphabetS[j];
            }
        }
    }
    return str;
}

bool Strings::isAllUnique(std::string&amp; str)
{
    int count[SIZE];
    for (int k = 0; k &lt; SIZE; k++)
    {
        count[k] = 0;
    }
    str = toLower(str);
    for (int i = 0; i &lt; str.length(); i++)
    {
        for (int j = 0; j &lt; SIZE; j++)
        {
            if (str[i] == alphabetS[j])
            {
                count[j] = count[j] + 1;
                if (count[j] &gt; 1)
                {
                    return false;
                }
                break;
            }
        }
    }
    return true;
}

int main()
{
    Strings obj;
    std::string str;
    std::cout &lt;&lt; "Enter String\n";
    std::getline(std::cin, str);
    bool res = obj.isAllUnique(str);
    if (res == true)
    {
        std::cout &lt;&lt; "There are unique characters in string\n";
    }
    else
    {
        std::cout &lt;&lt; "There are no unique characters in string\n";
    }
}
People are very eager to present their own solutions, so let me attempt an actual review of your code:


Your names could be clearer. Strings is not very descriptive, and neither are alphabetS/L. SIZE doesn't tell anyone what kind of size it is or what it relates to.
Anything not nested under private or public in a class is automatically private. So you might as well move those stray members into the private section to make things clearer.
If you're using C++11 or higher then you can use constexpr.
Why are you returning a value from your toLower function after you pass in a mutable reference? Might as well drop the return and use the reference.
Your toLower function does a lot of work. Assuming you don't want to use the STL for this you can still roll your own function. If you only want to check characters from A-z then you can simply get the lowercase by adding the difference to any number representation smaller than 97 (a). Look at the ASCII chart for reference.
You can simply initialize count with int count[SIZE]{};. Alternatively you could use memset.
Try to avoid performing more than one task in a function. E.g. factor out the toLower functionality of isAllUnique. It could then be const as it has no need to mutate class state.
For the actual algorithm I prefer Incomputable's approach over the one suggested by Ben Steffan which can be used without overly relying on the STL.
Prefer prefix over postfix for the increment operator.
Don't compare to true. Instead simply do if (a_bool_var), or if (!a_bool_var) for the negation.
This could probably be done with just free functions without needing a class object.