HackerRank Strong Password Problem Solution
In this post, we will solve HackerRank Strong Password Problem Solution.
Louise joined a social networking site to stay in touch with her friends. The signup page required her to input a name and a password. However, the password must be strong. The website considers a password to be strong if it satisfies the following criteria:
- Its length is at least 6.
- It contains at least one digit.
- It contains at least one lowercase English character.
- It contains at least one uppercase English character.
- It contains at least one special character. The special characters are:
!@#$%^&*()-+
She typed a random string of length n in the password field but wasn’t sure if it was strong. Given the string she typed, can you find the minimum number of characters she must add to make her password strong?
Note: Here’s the set of types of characters in a form you can paste in your solution:
numbers = "0123456789" lower_case = "abcdefghijklmnopqrstuvwxyz" upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" special_characters = "!@#$%^&*()-+"
Example
password = ‘2bbbb’
This password is 5 characters long and is missing an uppercase and a special character. The minimum number of characters to add is 2.
password = ‘2bb#A’
This password is 5 characters long and has at least one of each character type. The minimum number of characters to add is 1.
Function Description
Complete the minimumNumber function in the editor below.
minimumNumber has the following parameters:
- int n: the length of the password
- string password: the password to test
Returns
- int: the minimum number of characters to add
Input Format
The first line contains an integer n, the length of the password.
The second line contains the password string. Each character is either a lowercase/uppercase English alphabet, a digit, or a special character.
Sample Input 0
3 Ab1
Sample Output 0
3
Explanation 0
She can make the password strong by adding 3 characters, for example, $hk
, turning the password into Ab1$hk
which is strong.
2 characters aren’t enough since the length must be at least 6.
Sample Input 1
11 #HackerRank
Sample Output 1
1
Explanation 1
The password isn’t strong, but she can make it strong by adding a single digit.
Strong Password C Solution
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int minimumNumber(int n, char* password) {
// Return the minimum number of characters to make the password strong
int digit = 1;
int lcase = 1;
int ucase = 1;
int sgn = 1;
int len = strlen(password);
char charact[] = "!@#$%^&*()-+";
int rlt;
for(int i = 0; i < len; i++){
int j;
if(password[i] <= '9' && password[i] >= '0') digit = 0;
else if(password[i] >= 'a' && password[i] <='z') lcase = 0;
else if(password[i] >= 'A' && password[i] <='Z') ucase = 0;
else{
if(sgn == 1){
for(j = 0; j < 12; j++){
if(password[i] == charact[j]) break;
}
if(j < 12) sgn = 0;
}
}
rlt = digit + lcase + ucase + sgn;
if(rlt == 0) break;
}
if(rlt + len < 6) rlt = 6 - len;
return rlt;
}
int main() {
int n;
scanf("%i", &n);
char* password = (char *)malloc(512000 * sizeof(char));
scanf("%s", password);
int answer = minimumNumber(n, password);
printf("%d\n", answer);
return 0;
}
Strong Password C++ Solution
#include <bits/stdc++.h>
using namespace std;
#define si(x) scanf("%d", &x)
#define sll(x) scanf("%lld", &x)
#define PB push_back
#define MP make_pair
string A[4] = {"0123456789", "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "!@#$%^&*()-+"};
int main()
{
int n; string a;
cin >> n >> a;
int N = n;
int bt = 0;
for (auto c : a)
{
for (int i = 0; i < 4; i++)
if (A[i].find(c) != string::npos)
bt |= (1<<i);
}
for (int i = 0; i < 4; i++)
if (( (bt>>i) & 1 )== 0 )
n++;
if (n < 6) n = 6;
cout << n - N << endl;
return 0;
}
Strong Password C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static int minimumNumber(int n, string password) {
string numbers = "0123456789";
string lower_case = "abcdefghijklmnopqrstuvwxyz";
string upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string special_characters = "!@#$%^&*()-+";
int count=0;
if(!numbers.ToCharArray().Any(password.Contains))
count++;
if(!lower_case.ToCharArray().Any(password.Contains))
count++;
if(!upper_case.ToCharArray().Any(password.Contains))
count++;
if(!special_characters.ToCharArray().Any(password.Contains))
count++;
return Math.Max(6-n,count);
}
static void Main(String[] args) {
int n = Convert.ToInt32(Console.ReadLine());
string password = Console.ReadLine();
int answer = minimumNumber(n, password);
Console.WriteLine(answer);
}
}
Strong Password Java Solution
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static int minimumNumber(int n, String p) {
boolean d = false;
boolean u = false;
boolean l = false;
boolean q = false;
String s = "!@#$%^&*()-+";
String num = "0123456789";
String lower = "abcdefghijklmnopqrstuvwxyz";
String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int i=0; i<p.length(); i++){
if(!d && num.contains(""+p.charAt(i))){
d = true;
}
if(!l && lower.contains(""+p.charAt(i))){
l = true;
}
if(!u && upper.contains(""+p.charAt(i))){
u = true;
}
if(!q && s.contains(""+p.charAt(i))){
q = true;
}
}
int a = 0;
if(!d)
a++;
if(!l)
a++;
if(!u)
a++;
if(!q)
a++;
if(p.length()+a<6){
a = 6-p.length();
}
return a;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String password = in.next();
int answer = minimumNumber(n, password);
System.out.println(answer);
in.close();
}
}
Strong Password JavaScript Solution
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function minimumNumber(n, password) {
// Return the minimum number of characters to make the password strong
let as =password
let numbers = "0123456789"
let lower_case = "abcdefghijklmnopqrstuvwxyz"
let upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let special_characters = "!@#$%^&*()-+"
let required={
chars:1,
numbers:1,
lower_case:1,
upper_case:1
}
for(let a of as){
if(special_characters.includes(a)){
if(required.chars>0)
required.chars-=1
continue
}
if(numbers.includes(a)){
if(required.numbers>0)
required.numbers-=1
continue
}
if(lower_case.includes(a)){
if(required.lower_case>0)
required.lower_case-=1
continue
}
if(upper_case.includes(a)){
if(required.upper_case>0)
required.upper_case-=1
continue
}
}
let c = 0
for(let req in required){
c+=required[req]
}
return(Math.max(6-as.length,c))
}
function main() {
var n = parseInt(readLine());
var password = readLine();
var answer = minimumNumber(n, password);
process.stdout.write("" + answer + "\n");
}
Strong Password Python Solution
#!/bin/python3
import sys
def minimumNumber(n, password):
# Return the minimum number of characters to make the password strong
numbers = "0123456789"
lower_case = "abcdefghijklmnopqrstuvwxyz"
upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
special_characters = "!@#$%^&*()-+"
minLength = 6
toAdd = 0
toAdd2 = 0
if minLength > len(password):
toAdd += minLength-len(password)
if not any(a for a in numbers if a in password):
toAdd2 += 1
if not any(a for a in lower_case if a in password):
toAdd2 += 1
if not any(a for a in upper_case if a in password):
toAdd2 += 1
if not any(a for a in special_characters if a in password):
toAdd2 += 1
return(max(toAdd,toAdd2))
if __name__ == "__main__":
n = int(input().strip())
password = input().strip()
answer = minimumNumber(n, password)
print(answer)
Other Solutions