[Lintcode 1 easy]Unique Characters

【Problem】

Unique Characters

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

Example

Given "abc", return true.

Given "aab", return false.

Challenge

What if you can not use additional data structures?

---------------------------------------------------------------------------------------------------------------------

This problem is from <crack the coding interview>.

It is used to test the basic knowledge of ASCII.

If we didn‘t care about how much space we used, basic idea is to set a flag for every single char in the string. If the char has appeared once, return false.

Following is the code:

public class Solution {
    /**
     * @param str: a string
     * @return: a boolean
     */
    public boolean isUnique(String str) {

        // write your code here
        if(str.length()>128) return false;

        boolean[] char_set= new boolean[256];
        for(int i=0;i<str.length();i++)
        {
            int val=str.charAt(i);
            if(char_set[val])
            {
                return false;
            }
            else
            {
                char_set[val]=true;
            }
        }
        return true;

    }
}
时间: 2024-12-16 13:48:38

[Lintcode 1 easy]Unique Characters的相关文章

Determine if a string has all unique characters

Problem: Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? <pre name="code" class="cpp">#include<iostream> #include<set> #include<string&

[CareerCup] 1.1 Unique Characters of a String 字符串中不同的字符

1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structure? 这道题让我们判断一个字符串中是否有重复的字符,要求不用特殊的数据结构,这里应该是指哈希表之类的不让用.像普通的整型数组应该还是能用的,这道题的小技巧就是用整型数组来代替哈希表,在之前Bitwise AND of Numbers Range 数

LintCode: Unique Characters

C++, time: O(n^2) space: O(0) class Solution { public: /** * @param str: a string * @return: a boolean */ bool isUnique(string &str) { // write your code here for (int i=0; i<str.size(); i++) { for (int j=i+1; j<str.size(); j++) { if (str[i] ==

lintcode 容易题:Unique Characters 判断字符串是否没有重复字符

题目: 判断字符串是否没有重复字符 实现一个算法确定字符串中的字符是否均唯一出现 样例 给出"abc",返回 true 给出"aab",返回 false 挑战 如果不使用额外的存储空间,你的算法该如何改变? 解题: 定义一个集合最简单. Java程序: public class Solution { /** * @param str: a string * @return: a boolean */ public boolean isUnique(String st

[LintCode] First Position Unique Character

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Example Given s = "lintcode", return 0. Given s = "lovelintcode", return 2. Solution 1. Two steps' iteration 1. first t

[lintcode 2 easy Reverse Words in a String]

Problem Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Clarification What constitutes a word? A sequence of non-space characters constitutes a word. Could the

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

思路: 假设给定字符串用的是ASCII编码,那么总共就只有256个字符,新建一个256个元素的boolean数组, 遍历字符串,将出现的字符在boolean数组所在位置置 1.如果碰到已经置一,表明出现重复字符,返回false. public class IsUniqueChars_1 { public boolean solu(String s) { boolean table[] = new boolean[256]; for (int i = 0; i < s.length(); i++)

Longest Substring with At Most K Distinct Characters

Given a string, find the longest substring that contains only two unique characters. For example, given "abcbbbbcccbdddadacb", the longest substring that contains k unique character is "bcbbbbcccb". 分析: 用hashmap记录每个character从start到当前位置

LeetCode题828 —— Unique Letter String

https://leetcode.com/problems/unique-letter-string/description/ A character is unique in string S if it occurs exactly once in it. For example, in string S = "LETTER", the only unique characters are "L" and "R". Let's define