Array Question 1 unique string
2015-11-23 01:26
302 查看
Array question 1:
Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures.
Because we are not allowed to use additional data structures. so we could not use array to store the checking result.
The first thing I can think of is scanning the characters of the string one by one and compare it with all the characters after it. But it will cost us O(n^2) time. So we can use a structure like array to check it. Use the value of the character as the index
of the array and if there is another same character, then the corresponding array will already has value. So we can know the characters of the string is not unique.
1). Pay attention to attach " #include<string>"
2). There are so many functions that you can use in the C++ string library. Like size() , at(), length(), etc;
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cout<<"please input your string:"<<endl;
cin>>s;
int i;
int* b;
for(i=0;i<s.size();i++)
{
char a;
a=s.at(i);
if(b[a])
{
cout<<"The string is ununique!"<<endl;
return 0;
}
else
{
b[a]=1;
}
}
cout<<"The string is unique!";
}
time: O(n) Space: O(1)
Using bit vector:
There is another more convenient way to save the memory. We can use an integer to store the checking result. Transform each character into one bit in the integer "1" using"<<'(shift) and "|" . Check using "&".
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cout<<"please input your string:"<<endl;
cin>>s;
int i;
int check=0;
for(i=0;i<s.size();i++)
{
int a;
a=s.at(i)-'a'; //the character will be tansformed to 1 to 26
if((1 << a) & check) //shift 1 to the position of index a
{
cout<<"The string is ununique!"<<endl;
return 0;
}
else
{
check=(1<<a)|check;
}
}
cout<<"The string is unique!";
}
time O(n), space O(1)
Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures.
Because we are not allowed to use additional data structures. so we could not use array to store the checking result.
The first thing I can think of is scanning the characters of the string one by one and compare it with all the characters after it. But it will cost us O(n^2) time. So we can use a structure like array to check it. Use the value of the character as the index
of the array and if there is another same character, then the corresponding array will already has value. So we can know the characters of the string is not unique.
1). Pay attention to attach " #include<string>"
2). There are so many functions that you can use in the C++ string library. Like size() , at(), length(), etc;
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cout<<"please input your string:"<<endl;
cin>>s;
int i;
int* b;
for(i=0;i<s.size();i++)
{
char a;
a=s.at(i);
if(b[a])
{
cout<<"The string is ununique!"<<endl;
return 0;
}
else
{
b[a]=1;
}
}
cout<<"The string is unique!";
}
time: O(n) Space: O(1)
Using bit vector:
There is another more convenient way to save the memory. We can use an integer to store the checking result. Transform each character into one bit in the integer "1" using"<<'(shift) and "|" . Check using "&".
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cout<<"please input your string:"<<endl;
cin>>s;
int i;
int check=0;
for(i=0;i<s.size();i++)
{
int a;
a=s.at(i)-'a'; //the character will be tansformed to 1 to 26
if((1 << a) & check) //shift 1 to the position of index a
{
cout<<"The string is ununique!"<<endl;
return 0;
}
else
{
check=(1<<a)|check;
}
}
cout<<"The string is unique!";
}
time O(n), space O(1)
相关文章推荐
- Android中子线程中更新UI的N中方法
- UVA 11775 Unique Story(DP)
- Cocoapods的pod install出现的某个错误 but they required a higher minimum deployment target.
- fastcgi_finish_request() 正确使用方式
- uitableview的空白处不能响应 touchesbegan 事件
- 【转载】COM 组件设计与应用(二)——GUID 和 接口
- android面试题 包括UI控件及数据存储内容
- iOS setValuesForKeysWithDictionary
- iOS:自定义模态动画 --UIPresentationController
- HDU 1297 Children’s Queue
- Server ran out of threads to serve requests. Consider raising the ThreadsPerChild setting的解决方法
- leetCode+Unique Paths II
- leetcode Implement Queue using Stacks
- scala的futue和promise
- HDU 1005 Number Sequence
- iOS中UIButton的titleEdgeInsets和imageEdgeInsets
- EqualsBuilder和HashCodeBuilder
- 不同浏览器对URI以及queryString是如何编码的?
- 模块化以及requirejs和seajs
- BestCoder Round #63 (div.2) HDOJ5568 sequence2(dp + 大数)