Problem
Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?
Example1
Input: s = “leetcode”
Output: false
Example2
Input: s = “abc”
Output: true
Solution
哈希表或排序。
class Solution {
public:
bool isUnique(string astr) {
if(astr.empty())
return true;
sort(astr.begin(),astr.end());
for(int i = 0;i<astr.size() - 1;++i)
{
if(astr[i] == astr[i+1])
return false;
}
return true;
}
};