-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
69 lines (61 loc) · 1.59 KB
/
main.cpp
File metadata and controls
69 lines (61 loc) · 1.59 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <vector>
#include <unordered_map>
#include <cmath>
bool isLetter(char c);
bool isDigit(char c);
std::vector<int> buildDiff(std::vector<char> vec) {
int delta = 0;
std::vector<int> result;
for (char c: vec) {
if (isDigit(c)) {
delta++;
} else if (isLetter(c)) {
delta--;
}
result.push_back(delta);
}
return result;
}
std::pair<int, int> longestMatch(std::vector<int> diff) {
std::unordered_map<int, int> map;
std::pair<int, int> pair {0, 0};
for (int i = 0; i < diff.size(); i++) {
int value = diff[i];
if (map.count(value) > 0) {
int match = map[value];
int distance = std::abs(match - i);
int longest = pair.first - pair.second;
if (distance > longest) {
pair = {match, i};
}
} else {
map[value] = i;
}
}
return pair;
}
std::vector<char> findLongestSequence(std::vector<char> chars) {
auto diff = buildDiff(chars);
auto pair = longestMatch(diff);
std::vector<char> result;
for (int i = pair.first + 1; i <= pair.second; i++) {
result.push_back(chars[i]);
}
return result;
}
bool isLetter(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
bool isDigit(char c) {
return c >= '0' && c <= '9';
}
int main() {
std::vector<char> chars {'a', '1', '2', '3', 'z', 'b'};
auto vec = findLongestSequence(chars);
for (auto c: vec) {
std::cout << c << " ";
}
std::cout << std::endl;
return 0;
}