CareerCup-1.1

Question:

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

#include <iostream>
using namespace std;

bool isUnique(char* s)
{
    bool count[256];
    for(int i=0; i<256; i++)
        count[i] = false; 
    for(int i=0; i<strlen(s); i++)
    {
        if(count[s[i]])
            return false;
        count[s[i]] = true;
    }
    return true;
};

int main()
{
    cout<<isUnique("adce")<<endl;
    system("pause"); 
};

Comments

comments powered by Disqus