检查字符串中的唯一字符

时间:2016-12-26 23:07:09

标签: c++ string char

我正在尝试编写一个代码来检查字符串是否由唯一字符组成。我在前几行中进行了一些数据验证,并输入具有这些条件(长度= 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";
}

我认为这是一个逻辑错误,但我无法弄清楚。有什么想法吗?

5 个答案:

答案 0 :(得分:2)

如果您只关心检查容器是否仅包含唯一元素而您不想对其进行排序,则可以将数据复制到std::setstd::set只会存储唯一的项目,因此如果您使用的容器与您正在使用的容器不匹配,则填充该集合后,您就知道存在重复项。

使用迭代器构造函数检查字符串是否只包含唯一元素就像

一样简单
std::string line = "abcdefghijklmnopqrstuvwxyz0123456789";
std::set<char> checker(line.begin(), line.end());
if (checker.size() != line.size())
    std::cout << "contains duplicates!"; 

答案 1 :(得分:1)

var dataSet = [
  ["Tiger", "10", "20", "51", "3", "2", "1"],
  ["Hyena", "10", "20", "51", "3", "2 ", "1 "]
];
var colname1 = [{
  title: "Column0"
}, {
  title: "column1"
}, {
  title: "column2"
}, {
  title: "column3"
}, {
  title: "column4"
}];

colArray = [];
colArray.push({
  title: "Name"
});
$.each(colname1, function(index, value) {
  colArray.push({
    title: value.title
  });
});
colArray.push({
  title: "lastcol"
});

$(document).ready(function() {
  var t = $('#example').DataTable({
    data: dataSet,
    columns: colArray
  });
});

最后,请注意,有专门用于重复管理的标准工具,例如<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" rel="stylesheet"> <script src="//code.jquery.com/jquery-1.12.4.js"></script> <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script> <div> <table width="300" class="display" id="example"> <thead></thead> <tbody></tbody> </table> </div>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; } } } ......

另请注意,您的算法不是此任务的最快速度。

答案 2 :(得分:0)

你需要打破循环:

if (string[i] == string[j]) {
    uniqueCharacters = false;
    // here you need to break the loop
}
else {uniqueCharacters = true;}

否则,在检测到非唯一值之后,可以通过下一次循环迭代来重​​写它。

有更简单的方法来检查字符串uniqnes。算法中有std::unique

您还可以将每个字符移至std::set并检查字符串大小是否等于std::set大小。

答案 3 :(得分:-1)

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;
}

答案 4 :(得分:-2)

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";
}