Golden programmer interview - face questions 01.01 determine whether the unique character

topic:

https://leetcode-cn.com/problems/is-unique-lcci/

Whether to implement an algorithm to determine a string s of all the characters are all different.

Example 1:

Input: s = "leetcode"
Output: false
Example 2:

Input: s = "abc"
Output: true
limits:

0 <= len (s) < = 100
If you do not use extra data structure, will be a plus.

analysis:

Hash practices, opening up an array of characters corresponding to the position of number of occurrences statistics, traversing the string, when the number is 1, said it had appeared, and there are repeated characters.

program:

class Solution {
    public boolean isUnique(String astr) {
        int[] a = new int[128];
        for(char ch:astr.toCharArray()){
            if(a[ch] == 1)
                return false;
            a[ch] = 1;
        }
        return true;
    }
}

 

Guess you like

Origin www.cnblogs.com/silentteller/p/12389505.html