Home » C++ » Leet Code 420 Strong Password Checker (HARD)

# Leet Code 420 Strong Password Checker (HARD)

Leet Code . 420 Strong Password Checker: Hey there, coding enthusiasts! Welcome back to another exciting coding session. Today’s problem is a treat—literally! We’re going to solve the “Strong Password Checker” or “Leet Code .420

## Approach

• A strong password should contain at least one lowercase letter, one uppercase letter, and one number.
• It should avoid having the same character repeated three times consecutively and be between 6 and 20 characters long.

The code achieves this by following these steps:

1. Utilizes a “requirements” checklist to keep track of the first three criteria (initially unchecked).
2. Maintains a record of character repetitions.
3. Examines each character in the text:
• If different from the previous character, checks off requirements if not met.
• If the same as the previous character, counts consecutive repetitions.
4. Sorts the repeated characters list by the number of repetitions.
• For text longer than 20 characters, removes one repeated character in sequences of three or more.
• For texts without such sequences, removes one character from the repeated character list.
• Continues until the text is 20 characters or less.
6. Calculates the number of characters needed to make the text at least 6 characters long (if it’s shorter).
7. Totals all the changes made to meet the rules, providing the minimum number of changes required to create a strong password.

LeetCode 420 Strong Password Checker : C++ / Java / C# / C /Python

### C++ Solution Leet Code 420

``````// C++ Solution Code Here
class Solution {
public:
bitset<3> requirements{111};
list<int> repeats;
auto it = s.begin();
auto it2 = s.end();
while (it != s.end()) {
if (*it != *it2) {
if (requirements.test(0) && islower(*it))
requirements.reset(0);
if (requirements.test(1) && isupper(*it))
requirements.reset(1);
if (requirements.test(2) && isdigit(*it))
requirements.reset(2);
} else {
while (it != s.end() && *it == *it2)
++it;
if (distance(it2, it) != 2)
repeats.push_back(distance(it2, it));
if (it != s.end())
continue;
else
break;
}
it2 = it;
++it;
}
repeats.sort([](const int &lhs, const int &rhs) { return (lhs % 3) < (rhs % 3); });
int ans{0}, len{static_cast<int>(s.size())};
while (len > 20) {
if (!repeats.empty()) {
if (repeats.front() == 3) {
repeats.pop_front();
}
else {
--repeats.front();
repeats.sort([](const int &lhs, const int &rhs) { return (lhs % 3) < (rhs % 3); });
}
++ans;
--len;
}
else {
ans += len - 20;
len = 20;
}
}
int rep_ins{0};
while (!repeats.empty()) {
rep_ins += repeats.front() / 3;
repeats.pop_front();
}
if ((len + rep_ins) < 6) {
rep_ins += 6 - len - rep_ins;
}
ans += max(static_cast<int>(requirements.count()), rep_ins);
return ans;
}
};``````

Leet Code : Longest Palindromic Substring Java | CPP | Java Script | Python Solution

Code Explanation :

JAVA solution

### Java Solution Leet Code 420

``````// Java Solution Code Here
class Solution {
int res = 0, a = 1, A = 1, d = 1;
char[] carr = s.toCharArray();
int[] arr = new int[carr.length];
for (int i = 0; i < arr.length;) {
if (Character.isLowerCase(carr[i])) a = 0;
if (Character.isUpperCase(carr[i])) A = 0;
if (Character.isDigit(carr[i])) d = 0;
int j = i;
while (i < carr.length && carr[i] == carr[j]) i++;
arr[j] = i - j;
}

int total_missing = (a + A + d);
if (arr.length < 6) {
res += total_missing + Math.max(0, 6 - (arr.length + total_missing));
} else {
int over_len = Math.max(arr.length - 20, 0), left_over = 0;
res += over_len;
for (int k = 1; k < 3; k++) {
for (int i = 0; i < arr.length && over_len > 0; i++) {
if (arr[i] < 3 || arr[i] % 3 != (k - 1)) continue;
arr[i] -= Math.min(over_len, k);
over_len -= k;
}
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= 3 && over_len > 0) {
int need = arr[i] - 2;
arr[i] -= over_len;
over_len -= need;
}
if (arr[i] >= 3) left_over += arr[i] / 3;
}
res += Math.max(total_missing, left_over);
}
return res;
}
}``````

C# solution

### C# Solution Leet Code 420

``````// C# Solution Code Here
class Solution {
int charSum = GetRequiredChar(s);
if (s.Length < 6) return Math.Max(charSum, 6 - s.Length);
int replace = 0, ones = 0, twos = 0;
for (int i = 0; i < s.Length;) {
int len = 1;
while (i + len < s.Length && s[i + len] == s[i + len - 1]) len++;
if (len >= 3) {
replace += len / 3;
if (len % 3 == 0) ones += 1;
if (len % 3 == 1) twos += 2;
}
i += len;
}
if (s.Length <= 20) return Math.Max(charSum, replace);
int deleteCount = s.Length - 20;
replace -= Math.Min(deleteCount, ones);
replace -= Math.Min(Math.Max(deleteCount - ones, 0), twos) / 2;
replace -= Math.Max(deleteCount - ones - twos, 0) / 3;
return deleteCount + Math.Max(charSum, replace);
}
int GetRequiredChar(string s) {
int lowerCase = 1, upperCase = 1, digit = 1;
foreach (var c in s) {
if (char.IsLower(c)) lowerCase = 0;
else if (char.IsUpper(c)) upperCase = 0;
else if (char.IsDigit(c)) digit = 0;
}
return lowerCase + upperCase + digit;
}
}
``````

C solution

### C Solution Leet Code 420

``````// C Solution Code Here
int offences1 = 0;
int offences2 = 0;
int offences = 0;
int r = 0;
char prev = '\n';
char curr;
int repC = 1;
int delrep[3] = {0};
int maxrep = 0;
for(int i = 0; i < length; i++) {
if(curr == prev) {
repC++;
} else {
if(repC >= 3) {
delrep[repC%3]++;
}
repC = 1;
prev = curr;
}
if(repC % 3 == 0) {
r++;
}
if(repC > 3 && repC % 3 == 1) {
delrep[2]++;
}
}
if(repC >= 3) {
delrep[repC%3]++;
}
int v = 0;
int vo = 0;
int lc = 0;
int uc = 0;
int dc = 0;
for(int i = 0; i < length; i++) {
if(lc == 0 && islower(password[i]) != 0) {
v++;
lc = 1;
}
if(uc == 0 && isupper(password[i]) != 0) {
v++;
uc = 1;
}
if(dc == 0 && isdigit(password[i]) != 0) {
v++;
dc = 1;
}
}
if(v < 3) {
offences2 += 3 - v;
vo += 3 - v;
if(length == 5 && v == 1) {
offences1++;
}
}
if(length < 6 || length > 20) {
int delete = length - 20;
if(delrep[0] != 0) {
delrep[0] = delrep[0] < delete ? delrep[0] : delete;
}
if(delrep[1] != 0) {
delrep[1] = delrep[1] < (delete - delrep[0])/2 ? delrep[1] : (delete - delrep[0])/2;
}
if(delrep[2] != 0) {
delrep[2] = delrep[2] < ((delete - delrep[0]) - delrep[1]*2)/3 ? delrep[2] : ((delete - delrep[0]) - delrep[1]*2)/3;
}
r-= delrep[0] + delrep[1] + delrep[2];
int x = r > offences2 ?  r : offences2;
return length < 6 ? 6 - length + offences1 : delete + x;
}
return vo < r ? r : vo;
}``````

Python solution

### Python Solution Leet Code 420

``````# Python Solution Code Here
class Solution(object):
missing_type = 3
if any('a' <= c <= 'z' for c in s): missing_type -= 1
if any('A' <= c <= 'Z' for c in s): missing_type -= 1
if any(c.isdigit() for c in s): missing_type -= 1
change = 0
one = two = 0
p = 2
while p < len(s):
if s[p] == s[p-1] == s[p-2]:
length = 2
while p < len(s) and s[p] == s[p-1]:
length += 1
p += 1
change += length / 3
if length % 3 == 0: one += 1
elif length % 3 == 1: two += 1
else:
p += 1
if len(s) < 6:
return max(missing_type, 6 - len(s))
elif len(s) <= 20:
return max(missing_type, change)
else:
delete = len(s) - 20
change -= min(delete, one)
change -= min(max(delete - one, 0), two * 2) / 2
change -= max(delete - one - 2 * two, 0) / 3
return delete + max(missing_type, change)``````

This code is a concise and efficient solution to the “Strong Password Checker” problem, taking into account the constraints and requirements specified in the problem statement. It effectively calculates the minimum number of changes required to make a given string a strong password.

List of Some important Leet code Questions: