Skip to content

Instantly share code, notes, and snippets.

@ThunderXu
Last active December 14, 2015 02:29
Show Gist options
  • Save ThunderXu/5013616 to your computer and use it in GitHub Desktop.
Save ThunderXu/5013616 to your computer and use it in GitHub Desktop.
Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?
#include "stdafx.h"
#include <string>
#include <iostream>
bool HasDuplication(std::string);
int main()
{
using namespace std;
string str;
cin>>str;
bool res = HasDuplication(str);
if(res)
{
cout<<"Has Duplication"<<endl;
}
else
{
cout<<"Has All unique characters"<<endl;
}
return 0;
}
bool HasDuplication(std::string str)
{
for(int i=0,size=str.size();i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(str[i]==str[j])
{
return true;
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment