-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLongestPassword.cpp
More file actions
50 lines (45 loc) · 1.52 KB
/
LongestPassword.cpp
File metadata and controls
50 lines (45 loc) · 1.52 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
#include <cctype>
#include <algorithm>
#include <string>
using namespace std;
//https://codility.com/demo/results/training2NX55N-H6S/
/*
Longest Password
Observation:
Just one thing: it's a common mistake for not checking the last count with the ans due to the fact that the max check will
not occur if we reach the end of the string so the last count is missed.
That has been said, the first version below doesn't need the caution since the outter infinite loop guarantees that every count
will be checked and compared for max.
*/
int solutionLongestPassword(string &S) {
long long cntAlpha = 0, cntDigit = 0, cnt = 0, ans = -1, len = S.size() * -1LL;
auto end = S.end(), itor = S.begin();
while (1) {
cntDigit = cntAlpha = cnt = 0;
while (itor != end && ' ' != *itor) {
if (isdigit(*itor))++cntDigit;
else if (isalpha(*itor))++cntAlpha;
else cnt = len;
++itor, ++cnt;
}
if (cnt > 0 && cntDigit & 1 && !(cntAlpha & 1))ans = std::max(ans, cnt);
if (end == itor || end == ++itor)break;
}
return ans;
}
int solutionLongestPassword1(string &S) {
long long cntAlpha = 0, cntDigit = 0, cnt = 0, ans = -1, len = S.size() * -1LL;
auto end = S.end(), itor = S.begin();
while (end != itor) {
if (isdigit(*itor))++cntDigit;
else if (isalpha(*itor))++cntAlpha;
else if (' ' == *itor) {
if (cnt > 0 && cntDigit & 1 && !(cntAlpha & 1))ans = std::max(ans, cnt);
cntDigit = cntAlpha = 0;
cnt = -1;
}
else cnt = len;
++itor, ++cnt;
}
return std::max(ans, cnt > 0 && cntDigit & 1 && !(cntAlpha & 1) ? cnt : -1);
}