leetcode/lcci/01.01.Is Unique
..
README.md
README_EN.md
Solution.java
Solution.py

README_EN.md

01.01. Is Unique

中文文档

Description

Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?

Example 1:


Input: s = "leetcode"

Output: false

Example 2:


Input: s = "abc"

Output: true

Note:

  • 0 <= len(s) <= 100

Solutions

Python3

class Solution:
    def isUnique(self, astr: str) -> bool:
        sets = set(astr)
        return len(sets) == len(astr)

Java

class Solution {
    public boolean isUnique(String astr) {
        char[] chars = astr.toCharArray();
        int len = chars.length;
        for (int i = 0; i < len - 1; ++i) {
            for (int j = i + 1; j < len; ++j) {
                if (chars[i] == chars[j]) {
                    return false;
                }
            }
        }
        return true;
    }
}

...