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>
#include<algorithm>
#include<memory.h>
#include<fstream>
#include<ctime>
#include<bitset>
using namespace std;

// Solution1: 使用set
bool isUnique1(string str)
{
    set<char> s;
    int sizeOfStr = str.size();
    for(int i=0; i<sizeOfStr; ++i)
        s.insert(str[i]);
    return s.size()==sizeOfStr;
}

// Solution2: 从第二个字符开始判断,该字符与在它之前的每一个字符进行比较,如果相等,则返回false
bool isUnique2(string str)
{
    if(str.empty() || str.size()==1) return true;
    for(int i=1; i<str.size(); ++i)
    {
        for(int j=0; j<i; ++j)
            if(str[i] == str[j]) return false;
    }
    return true;
}

// Solution3: 先排序,再对相邻字符进行比较
bool isUnique3(string str)
{
    if(str.empty() || str.size()==1) return true;
    sort(str.begin(), str.end());
    for(int i=0; i<str.size()-1; ++i)
        if(str[i] == str[i+1]) return false;
    return true;
}

// Solution4: 用128=2^7大小的字符数组存放字符,如果当前遍历的字符已经存放过,则表明
// 有重复的字符
bool isUnique4(string str)
{
    if(str.empty() || str.size()==1) return true;
    char arr[128];
    memset(arr, 0, 128);
    for(int i=0; i<str.size(); ++i)
    {
        int num = (int)str[i];
        if(arr[num] != 0) return false;
        arr[num]++;
    }
    return true;
}

// Solution5: 使用bitset可比Solution4进一步减少额外使用空间,bitset只需考虑字符串中字符的ASCII码范围
bool isUnique5(string str)
{
    if(str.empty() || str.size()==1) return true;
    bitset<128> bs;
    for(int i=0; i<str.size(); ++i)
    {
        int num = (int)str[i];
        if(bs[num]) return false;
        bs[num] = 1;
    }
    return true;
}

int main()
{
    clock_t start = clock();
    ifstream ifile("strings.txt");
    string str;
    while(getline(ifile, str))
    {
        if(isUnique5(str)) printf("%s is unique.\n", str.c_str());
        else printf("%s is not unique.\n", str.c_str());
    }
    clock_t finish = clock();
    double time = (double)(finish-start)/CLOCKS_PER_SEC*1000;
    printf("This program runs %fms.\n", time);
    return 0;
}
				
时间: 2024-10-29 03:19:16

Determine if a string has all unique characters的相关文章

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++)

[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 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? ---------

动态规划-独特的子字符串存在于Wraparound String总个数 Unique Substrings in Wraparound String

2018-09-01 22:50:59 问题描述: 问题求解: 如果单纯的遍历判断,那么如何去重保证unique是一个很困难的事情,事实上最初我就困在了这个点上. 后来发现是一个动态规划的问题,可以将每个字符结尾的最长长度进行保存,这样就巧妙的解决的重复的问题. The max number of unique substring ends with a letter equals to the length of max contiguous substring ends with that

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

CC150-Array and string 1.1

Promble: Implement an algorithm to determine if a string has all unique characters. What if you cannot use addtional data structure? My solution: 1.ckeck wether the length of str is really equal to 0 or more than 256, if yes, it will return false 2.

string类问题总结

1. Unique Characters of a String 字符串中不同的字符 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structure? 思路分析:这道题让我们判断一个字符串中是否有重复的字符,要求不用特殊的数据结构,这里应该是指哈希表之类的不让用.像普通的整型数组应该还是能用的,这道题的小技巧就是用

Cracking the coding interview汇总目录

很久之前刷的CTCI的题目,都快忘记了,做个分类汇总,再重新好好复习一遍. Chapter 1 | Arrays and Strings 1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? 1.2 Write code to reverse a C-Style String. (C-Str