Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isUniqueChar_noDS is now O(nlogn) #46

Merged
merged 1 commit into from
Nov 8, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions Ch 1.Arrays And Strings/1.Is Unique/1. Is_unique.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <vector>
#include <iostream>
#include <bitset>
#include <algorithm> // for sort()

using namespace std;

bool isUniqueChars(const string &str){
Expand Down Expand Up @@ -32,15 +34,19 @@ bool isUniqueChars_bitvector(const string &str) {
}
return true;
}
bool isUniqueChars_noDS(const string &str) {
for(int i = 0; i < str.length()-1; i++) {
for(int j = i+1; j < str.length(); j++) {
if(str[i] == str[j]) {
return false;
}
bool isUniqueChars_noDS( string str) {

sort(str.begin(), str.end()); // O(nlogn) sort from <algorithm>

bool noRepeat = true;
for ( int i = 0 ; i < str.size() - 1; i++){
if ( str[i] == str[i+1] ){
noRepeat = false;
break;
}
}
return true;

return noRepeat;
}

int main(){
Expand All @@ -57,7 +63,7 @@ int main(){
cout <<endl << "Using no Data Structures" <<endl;
for (auto word : words)
{
cout << word << string(": ") << boolalpha << isUniqueChars_bitvector(word) <<endl;
cout << word << string(": ") << boolalpha << isUniqueChars_noDS(word) <<endl;
}
return 0;
}
Expand Down