lintcode-easy-Unique Characters

Implement an algorithm to determine if a string has all unique characters.

Given "abc", return true.

Given "aab", return false.

public class Solution {
    /**
     * @param str: a string
     * @return: a boolean
     */
    public boolean isUnique(String str) {
        // write your code here
        if(str == null || str.length() == 0)
            return true;
        
        int[] ch = new int[128];
        
        for(int i = 0; i < str.length(); i++){
            ch[str.charAt(i)]++;
            
            if(ch[str.charAt(i)] > 1)
                return false;
        }
        
        return true;
    }
}

 

posted @ 2016-03-09 08:44  哥布林工程师  阅读(143)  评论(0编辑  收藏  举报