Determing if string has all unique characters

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

bool is_all_unique(std::string str ) {
	bool char_set[256] = { false };
	bool rc = false;
	for ( int idx = 0; idx < (int)str.length(); idx++) {
		int ascii_code = (int)str[idx];
		if (char_set[ascii_code]){
			rc = true;
			 break;
		}
		char_set[ascii_code] = true;
	}
	return rc;
}
Advertisements

One thought on “Determing if string has all unique characters

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s