-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq17.5.cpp
More file actions
73 lines (57 loc) · 1.64 KB
/
Copy pathq17.5.cpp
File metadata and controls
73 lines (57 loc) · 1.64 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
70
71
72
73
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
bool isKeyExists(int key, map<int, int> &hashmap) {
if (hashmap.find(key) == hashmap.end())
return false;
else
return true;
}
void get_max_subarray_equal(string data) {
map<int, int> diff_map;
int letterCount = 0, numberCount = 0, max_len = 0, lindex = 0;
int diff;
char character;
cout << data << endl;
for (int it = 0; it < (int)data.length(); it++) {
character = data[it];
// cout << character << " " << (int)character;
if ((int)character >= 49 && (int)character <= 57) // it is a number
numberCount++;
else
letterCount++;
diff = numberCount - letterCount;
// cout << " " << diff << endl;
if (isKeyExists(diff, diff_map)) {
if ((it - diff_map[diff]) > max_len) {
max_len = it - diff_map[diff];
lindex = diff_map[diff];
}
}
else {
diff_map[diff] = it;
}
}
cout << "max subarray with equal number of letters and numbers is: "
<< max_len << endl;
cout << "the subarray string lindex: " << lindex + 1 << endl;
cout << "the subarray string is: " << data.substr(lindex + 1, max_len)
<< endl;
return;
}
int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) {
string in1 = "ab1c4e23d89gf";
string in2 = "a12c34b789d";
string in3 = "a123456b";
string in4 = "a1234c56b";
get_max_subarray_equal(in1);
cout << endl;
get_max_subarray_equal(in2);
cout << endl;
get_max_subarray_equal(in3);
cout << endl;
get_max_subarray_equal(in4);
return 0;
}