-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnearest_repeated_entry.h
More file actions
44 lines (38 loc) · 1.54 KB
/
Copy pathnearest_repeated_entry.h
File metadata and controls
44 lines (38 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#ifndef CPP_ALGORITHM_NEAREST_REPEATED_ENTRY_H
#define CPP_ALGORITHM_NEAREST_REPEATED_ENTRY_H
#include <limits>
#include <string>
#include <unordered_map>
#include <vector>
namespace NearestRepeatedEntry
{
/**
* \brief Find the nearest repeated entry in an array of strings.
* \details Given an array of strings, find the distance between the closest pair of equal entries.
* \param paragraph an array of strings
* \return the distance between the closest pair of equal entries
*/
int FindNearestRepeatedEntry(
const std::vector<std::string>& paragraph);
}
// ----------------------------------------------------------------------------
inline int NearestRepeatedEntry::FindNearestRepeatedEntry(
const std::vector<std::string>& paragraph)
{
std::unordered_map<std::string, int> last_occurrence;
int nearest_repeated_distance = std::numeric_limits<int>::max();
// iterate over the array
for (int i = 0; i < static_cast<int>(paragraph.size()); ++i)
{
// if the current word has been seen before,
// update the nearest repeated distance
if (auto latest_equal_word = last_occurrence.find(paragraph[i]); latest_equal_word != last_occurrence.end())
{
nearest_repeated_distance = std::min(nearest_repeated_distance, i - latest_equal_word->second);
}
last_occurrence[paragraph[i]] = i;
}
// if no repeated words were found, return -1
return nearest_repeated_distance != std::numeric_limits<int>::max() ? nearest_repeated_distance : -1;
}
#endif