Determine if a string has all unique characters
来源:互联网 发布:屏幕记录软件 编辑:程序博客网 时间:2024/05/21 18:24
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: 使用setbool 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: 从第二个字符开始判断,该字符与在它之前的每一个字符进行比较,如果相等,则返回falsebool 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;}
0 0
- determine if a string has all unique characters
- Determine if a string has all unique characters
- 1.1 Implement an algorithm to determine if a string has all unique characters.
- 【1】Detect if a string has all unique characters
- //Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the in
- Unique Characters of a String
- Check if the string has unique char.
- LeetCode141:Given a linked list, determine if it has a cycle in it.
- how to determine if an iOS device has a cellular radio
- how to determine if an iOS device has a cellular radio
- Comparing Floats: How To Determine if Floating Quantities Are Close Enough Once a Tolerance Has Been Reached
- Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa
- Find the longest substring with k unique distinct characters in a given string (Google)
- Rearrange a string so that all same characters become d distance away minDistance priority queue
- Using BitSet to to judge if string a contains all letters in string b
- 【软件自动化测试 - VBScript 7】= Using Locate to determine if specific text exists within a string
- unique characters
- Determine if a user's Session is still active
- 忘记root密码的解决方法
- 大数据分析的八大趋势
- AVFoundation Tutorial: Adding Overlays and Animations to Videos
- Unix环境高级编程--进程控制
- 2.2 opencv CvMat矩阵学习
- Determine if a string has all unique characters
- SpringMVC介绍之Validation
- 中文分词下载IK Analyzer 2012FF_hf1
- 天翼云一处xxe漏洞可读取任意文件
- Android开发教程--关于系统相机拍照获取的照片尺寸较小问题的解决
- 创业路上喊你父母来解决传统自动化测试之痛
- 大数据思维的十大原理:当样本数量足够大时,你会发现其实每个人都是一模一样的
- Android利用canvas画各种图形
- LeetCode Implement Trie (Prefix Tree)