[英]Check for unique characters in a string
我正在尝试编写代码来检查字符串是否由唯一字符组成。 我在前几行中进行了一些数据验证,并在这些条件(长度= 1或长度> 36)下输入了代码。 当我输入的字符串不符合上述要求时,我试图将字符串的每个字符与其他每个字符进行比较。 即使对于唯一字符的字符串(如以下示例中的字符串),它也会返回该字符串不是由唯一字符组成的字符串。
string string = "abcdefghijklmnopqrstuvwxyz0123456789";
bool uniqueCharacters = false;
//if length is 1, automatically return that the string is made up of unique characters
if (string.length() == 1) {
uniqueCharacters = true;
}
//there are 26 letters and 10 numbers, so if a string is made up of more than 36 chars, there must be some overlap
if (string.length() > 36) {
uniqueCharacters = false;
}
else if (string.length() > 1 && string.length() < 37) {
for (int i = 0; i < string.length(); i++) {
for (int j = 1; j < string.length(); j++) {
if (string[i] == string[j]) {
uniqueCharacters = false;
}
else {uniqueCharacters = true;}
}
}
}
if (uniqueCharacters == true) {
cout << "This string contains all unique characters \n";
}
if (uniqueCharacters == false) {
cout << "This string does not contain all unique characters \n";
}
我认为这是一个逻辑错误,但我无法弄清楚。 有任何想法吗?
如果您只想检查容器是否仅包含唯一元素并且不想对其进行排序,则可以将数据复制到std::set
。 std::set
将仅存储唯一项,因此,如果大小与您使用的容器不匹配,则填充该集合后,您就会知道重复项。
使用迭代器构造函数检查一个字符串是否仅包含唯一元素就很简单
std::string line = "abcdefghijklmnopqrstuvwxyz0123456789";
std::set<char> checker(line.begin(), line.end());
if (checker.size() != line.size())
std::cout << "contains duplicates!";
bool uniqueCharacters = true; // <-- initialize to true then try to find if false
// We put the condition `uniqueCharacters` in the loop:
// no need to continue looping once we find a duplicate.
for (int i = 0; uniqueCharacters && i < string.length(); ++i) {
for (int j = i+1; uniqueCharacters && j < string.length(); ++j) { // <-- start with j=i+1, to avoid checking a character versus itself
if (string[i] == string[j]) {
uniqueCharacters = false;
}
}
}
最后,请注意,有一些专用于重复管理的std工具,例如std::unique
和std::set<>
...
还要注意,您的算法并不是执行此任务最快的方法。
您需要打破循环:
if (string[i] == string[j]) {
uniqueCharacters = false;
// here you need to break the loop
}
else {uniqueCharacters = true;}
否则,在检测到非唯一值之后,可以通过下一个循环迭代将其覆盖。
有一种更简单的方法来检查字符串uniqnes。 算法中有std::unique
。
您也可以将每个字符移到std::set
并检查字符串大小是否等于std::set
大小。
bool are_all_characters_unique(string str) {
int n=str.length();
std::sort(str.begin(),str.end());
for(int i=0;i<n;i++){
if(str[i]!=str[i++]){
return false;
}
i++;
}
return true;
}
string string = "abcdefghijklmnopqrstuvwxyz0123456789";
bool uniqueCharacters = false;
//if length is 1, automatically return that the string is made up of unique characters
if (string.length() == 1) {
uniqueCharacters = true;
}
//there are 26 letters and 10 numbers, so if a string is made up of more than 36 chars, there must be some overlap
if (string.length() > 36) {
uniqueCharacters = false;
}
else if (string.length() > 1 && string.length() < 37) {
for (int i = 0; i < string.length(); i++) {
for (int j = i+1; j < string.length(); j++) {
if (string[i] == string[j]) {
uniqueCharacters = false;
}
else {uniqueCharacters = true;}
}
}
}
if (uniqueCharacters == true) {
cout << "This string contains all unique characters \n";
}
if (uniqueCharacters == false) {
cout << "This string does not contain all unique characters \n";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.