1-1-hasUniqueChars Algorithm

The 1-1-hasUniqueChars Algorithm is a programming solution used to determine if a given string has all unique characters or not. This algorithm is helpful in various applications such as checking the validity of user-generated passwords, detecting repeated patterns in a sequence, or in cryptographic challenges. The main idea behind this algorithm is to iterate through the string and check if any character appears more than once. If a repeated character is found, the algorithm returns false, indicating that the string does not have unique characters. Otherwise, it returns true, stating that all the characters in the string are unique. There are several ways to implement the 1-1-hasUniqueChars Algorithm, and the implementation can vary based on the programming language and data structures used. One common method is to use a hash table or a set data structure, which supports quick look-up and insertion operations. When iterating through the string, the algorithm checks if the character is already present in the hash table or set. If it is, the algorithm immediately returns false. If not, the character is added to the data structure and the algorithm continues to the next character. Another approach is to sort the string and then check for consecutive identical characters in the sorted string. This method eliminates the need for additional data structures but may have higher time complexity due to the sorting step. Overall, the 1-1-hasUniqueChars Algorithm is an efficient technique for determining the uniqueness of characters in a string.
/*
 * Cracking the coding interview, edition 6
 * Problem 1.1
 * Write an algorithm to determine whether a string has unique characters or not
 * Can we do it without using additional data structures?
 */



#include <iostream>
#include <cstring>

/*
 * Function hasUniqueChars1
 * Args - std::string
 * Output:: True if string has all characters which are unique
 *          False if string has at least one repeated char.
 * Assumption:: ASCII chars, 256 chars.
 */
bool hasUniqueChars1( std::string str)
{
    int len = str.length();
    int check[8]; //represents 256 bits.
    std::memset( check, 0 , sizeof check );
    int idx, v, shift;

    for ( int i = 0; i < len; ++i) {
        v = (int) str[i];
        idx = v / 32;      //identify which int bucket will represent this char
        shift = v % 32;    //identify which bit in above int will represent the char 
        if ( check[idx] & ( 1 << shift ) ) {
            return false;
        }
        check[idx] |= ( 1 << shift );
    }
    return true;
}

/*
 * Function hasUniqueChars2
 * Args - std::string
 * Output:: True if string has all characters which are unique
 *          False if string has at least one repeated char.
 * Assumption:: string only contains (a..z), 26 chars.
 */

bool hasUniqueChars2( std::string str)
{
    int check = 0;
    int len = str.length();
    for (int i = 0; i < len; ++i)
    {
        int c = (int)(str[i] - 'a');
        if ( check & ( 1 << c ) ) {
            return false;
        }
        check |= ( 1 << c);
    }
    return true;
}


int main ()
{
    std::string word1("copyrightable");
    std::string word2("Dermatoglyphics!");
    std::string word3("abiogenesis");
    std::string word4("Centrobaric!");

    //a word with unique chars (a-z)
    std::cout << "Does " << word1 << " has unique chars :" << hasUniqueChars2(word1) << std::endl;

    //a word with unique ASCII chars
    std::cout << "Does " << word2 << " has unique chars :" << hasUniqueChars1(word2) << std::endl;

    //a word with repeated chars (a-z)
    std::cout << "Does " << word3 << " has unique chars :" << hasUniqueChars2(word3) << std::endl;
    
    //a word with repeated AsCII chars
    std::cout << "Does " << word4 << " has unique chars :" << hasUniqueChars1(word4) << std::endl;
    return 0;

}


LANGUAGE:

DARK MODE: