Implement an algorithm to determine s
whether all characters of a string are different.
Example 1:
Input: s = "leetcode" output: false
Example 2:
Input: s = "abc" output: true
code show as below:
class Solution {
public:
bool isUnique(string astr) {
int n=astr.size();
if(n>26)//一共只有26个字符,当长度大于26时,一定有重复的
{
return false;
}
sort(astr.begin(),astr.end());//对字符串进行排序,如果有相同的字符,在相邻的位置
for(int i=0;i<n-1;i++)
{
if(astr[i]==astr[i+1])
{
return false;//当相邻的位置有相同的字符,说明字符有重复的
}
}
return true;
}
};