Skip to content

Commit f328919

Browse files
committed
Add strong-pass-check-2
1 parent a48e37f commit f328919

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ add_subdirectory(letter-comb)
55
add_subdirectory(lgc)
66
add_subdirectory(parentheses)
77
add_subdirectory(remove-duplicates-from-sorted)
8+
add_subdirectory(strong-pass-check-2)
89
add_subdirectory(validate-ip)

strong-pass-check-2/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
set(CMAKE_C_STANDARD 99)
2+
3+
add_executable(strong-pass-check-2 main.c)

strong-pass-check-2/main.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <ctype.h>
2+
#include <stdbool.h>
3+
#include <stdio.h>
4+
#include <string.h>
5+
6+
static bool isPermittedSpecial(char ch)
7+
{
8+
switch (ch) {
9+
case '!':
10+
case '@':
11+
case '#':
12+
case '$':
13+
case '%':
14+
case '^':
15+
case '&':
16+
case '*':
17+
case '(':
18+
case ')':
19+
case '-':
20+
case '+':
21+
return true;
22+
default:
23+
return false;
24+
}
25+
}
26+
27+
bool strongPasswordCheckerII(char *password)
28+
{
29+
if (strlen(password) < 8) {
30+
return false;
31+
}
32+
33+
char last_ch = '\0';
34+
35+
bool has_lower = false;
36+
bool has_upper = false;
37+
bool has_digit = false;
38+
bool has_special = false;
39+
for (const char *ch = password; *ch != '\0'; ++ch) {
40+
if (*ch == last_ch) {
41+
return false;
42+
}
43+
last_ch = *ch;
44+
if (islower(*ch)) {
45+
has_lower = true;
46+
continue;
47+
}
48+
if (isupper(*ch)) {
49+
has_upper = true;
50+
continue;
51+
}
52+
if (isdigit(*ch)) {
53+
has_digit = true;
54+
continue;
55+
}
56+
if (isPermittedSpecial(*ch)) {
57+
has_special = true;
58+
continue;
59+
}
60+
}
61+
62+
return has_lower && has_upper && has_digit && has_special;
63+
}
64+
65+
/* https://leetcode.com/problems/strong-password-checker-ii/ */
66+
int main(void)
67+
{
68+
printf("d: %d\n", strongPasswordCheckerII("IloveLe3tcode!"));
69+
return 0;
70+
}

0 commit comments

Comments
 (0)