Implement an algorithm to determine whether all characters of a string s are all different.
1. Subject requirements
Example 1:
输入: s ="leetcode"
输出: false
Example two:
输入: s ="abc"
输出: true
limit:
0<=len(s)<=100
Two, sample algorithm
Implementation example one (Swift): Traverse the string, remove the first letter each time, and then query whether there is this removed first letter in the remaining string;
funcisUnique(_ astr:String)->Bool{
var s = astr
for subS in s {
s.removeFirst()if s.contains(subS){
returnfalse}}returntrue}
Implementation example two (C ): The i-th character is compared with the characters i+1 and after, if the same is found, false is returned directly, and there is no false return at the end of the loop, it means that everything is different, and true is returned;
bool isUnique(char* astr){
int cnt =0, i, j;
cnt =strlen(astr);for(i =0; i < cnt -1; i++)for(j = i +1; j < cnt; j++)if(astr[i]== astr[j])return false;return true;}
Implementation example three (C++):
boolisUnique(string astr){
for(int i =0; i < astr.length();++i){
for(int j = i+1; j < astr.length();++j){
if(astr[i]== astr[j])returnfalse;}}returntrue;}