LintCode - 判断字符串是否没有重复字符(普通)

版权声明:本文为博主原创文章,未经博主允许不得转载。

难度:容易
要求:

实现一个算法确定字符串中的字符是否均唯一出现
样例
给出"abc",返回 true
给出"aab",返回 false

思路:
用数组计数,用到了计数排序的思想

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

你可能感兴趣的:(LintCode - 判断字符串是否没有重复字符(普通))