Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?
实现一个算法,判断字符串中的所有字符是否唯一,但是不能使用额外的数据结构
/************************************************************************/
/* Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?
实现一个算法,判断字符串中的所有字符是否唯一,但是不能使用额外的数据结构
*/
/************************************************************************/
/************************************************************************/
/* solutions:假定字符是ACSII码 */
/************************************************************************/
#include <iostream>
#include <string>
using namespace std;
bool IsUniqueChar(string str);
int main()
{
string str;
cin>>str;
cout<<IsUniqueChar(str);
return 0;
}
bool IsUniqueChar(string str)
{
int wholeChar[256]={0};
for (int i=0;i<str.length();i++)
{
if (++wholeChar[str[i]]>1)
return false;
}
return true;
}