实现一个算法,确定一个字符串 s 的所有字符是否全都不同。
示例 1:
输入: s = “leetcode”
输出: false
示例 2:
输入: s = “abc”
输出: true
限制:
0 <= len(s) <= 100
如果你不使用额外的数据结构,会很加分。
#include <iostream>
#include <algorithm>
using namespace std;
bool isUnique(string astr) {
sort(astr.begin(), astr.end());
for (int i = 1; i < astr.length(); i++)
if (astr[i - 1] == astr[i])
return false;
return true;
}
int main() {
string str;
cin >> str;
if (isUnique(str))
cout << "True";
else
cout << "False";
return 0;
}