Interview question [simple]: Determine whether a character is uniquely parsed (python, Java)

From leetcode

Subject requirements:

Implement an algorithm to determine whether all characters of a string s are all different.

Restrictions :
· 0 <= len(s) <= 100
· If you don't use additional data structures, it will be a bonus.

Python solution:

	class Solution(object):
		def isUnique(self, astr):
			c = ""
        for i in astr:
            if i not in c:
                c += i
            else:
                return False
        return True

Java solution:

class Solution {
    
    
    public boolean isUnique(String astr) {
    
    
        Set set = new HashSet();
        for (int i = 0; i <astr.length() ; i++) {
    
    
            set.add(astr.charAt(i));
        }
        return set.size() == astr.length();
    }
}

Guess you like

Origin blog.csdn.net/qq_40463117/article/details/105514928