determine if a string has all unique characters?

前端 未结 16 1462
长发绾君心
长发绾君心 2020-12-28 08:29

Can anybody tell me how to implement a program to check a string contains all unique chars ?

16条回答
  •  清酒与你
    2020-12-28 09:21

    I hope this can help you

    #include 
    using namespace std;
    int main() {
     string s;
     cin>>s;
     int a[256]={0};
     int sum=0;
     for (int i = 0; i < s.length();i++){
        if(a[s[i]]==0)++sum;
        a[s[i]]+=1;
     }
     cout<<(sum==s.length()?"yes":"no");
     return 0;
    

    }

提交回复
热议问题