Determines if the string is unique

Implement an algorithm that determines whether all characters of a string s are different.

Example 1:

Input: s = “leetcode” Output: false Example 2:

Input: s = “ABC” Output: true

0 <= len(s) <= 100 bonus points if you don’t use extra data structures

java

class Solution {
    public boolean isUnique(String astr) {
        char astrs[] = astr.toCharArray();
        Arrays.sort(astrs);
        for(int i=0; i<astrs.length-1; i++){if(astrs[i]==astrs[i+1]) {return false; }}return true; }}Copy the code

python

class Solution(object) :
    def isUnique(self, astr) :
        astrs=sorted(astr)
        for i in range(len(astrs)-1) :if astrs[i]==astrs[i+1] :return  False
        return True
Copy the code

So the idea here is to sort the string, and then if there are duplicate characters that must be adjacent to each other, and then iterate through the string array and return false if the previous position is the same as the next and then return true, Note that Java uses toCharArray to convert a string into an array of strings