c++ – search algorithm in an array of strings most different from the first


I'm looking for the fastest algorithm to find the string in an array of strings that is the most different from the first string in the array. The lines are all the same length. The position of characters and their ASCII code is of fundamental importance. If you solve the problem head-on, then you need to go through each line of the array and compare each of its characters with the character of the first line with the same index. If they are different, then the counter for that row of the array is incremented. The largest counter gives us the line that is the most different from the first.


You can use the following algorithm:

std::vector<std::string> strings
std::vector<size_t> distances;
for(size_t i = 1; i < strings.size(); ++i)
    std::deque<bool> tmp(strings[i].size());
    for(size_t j = 0; j < tmp.size(); ++j)
        tmp[j] = static_cast<char>(strings[i][j] == strings[0][j]);
    auto distance = count(begin(tmp), end(tmp), false);
auto idx = distance(begin(distances), max_element(begin(distances), end(distances))) + 1;
auto result = strings[idx];

This code is very easy to rewrite for vector instructions and its speed can increase many times over, but for this you need to know the size of the lines (You know it, I don’t)

Scroll to Top