Strong Password
Problem Statement :
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 = "!@#$%^&*()-+" 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. Constraints 1 <= n <= 100
Solution :
Solution in C :
In C++14 :
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fr first
#define sc second
#define MAX ((ll)(1e18+100))
#define MOD ((ll)(1e9+7))
#define HS1 ((ll)(137))
#define HS2 ((ll)(1e9+9))
#define ARRS ((ll)(1e6+900))
#define pb push_back
#define mid ((l+r)>>1)
#define PI 3.14159265358979323846
int main(){
ll n;
string s;
cin>>n;
cin>>s;
ll d=1,l=1,u=1,sp=1;
for(int i=0; i<n; i++){
if('0'<=s[i]&&s[i]<='9')
d=0;
else if('a'<=s[i]&&s[i]<='z')
l=0;
else if('A'<=s[i]&&s[i]<='Z')
u=0;
else
sp=0;
}
cout<<max({0ll,d+l+u+sp,6-n});
return 0;
}
In Java :
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class ProblemA {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String s = in.next();
Matcher num,low,up,spec;
num = Pattern.compile("[0123456789]").matcher(s);
low = Pattern.compile("[abcdefghijklmnopqrstuvwxyz]").matcher(s);
up = Pattern.compile("[ABCDEFGHIJKLMNOPQRSTUVWXYZ]").matcher(s);
spec = Pattern.compile("[!@#$%^&*()-/+]").matcher(s);
int count = 0;
if(!num.find())
count++;
if(!low.find())
count++;
if(!up.find())
count++;
if(!spec.find())
count++;
if(n+count<6){
count+=6-n-count;
}
System.out.println(count);
}
}
In C :
#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) {
int L_c = 0;
int U_c = 0;
int no = 0;
int s_c = 0;
int len = strlen(password);
int i;
for(i = 0; i<n; i++)
{
if(password[i] >= 'a' && password[i] <= 'z')
L_c++;
else if(password[i] >= 'A' && password[i] <= 'Z')
U_c++;
else if(password[i] >= '0' && password[i] <= '9')
no++;
else
s_c++;
}
int add = 0;
if(L_c == 0)
add++;
if(U_c == 0)
add++;
if(no == 0)
add++;
if(s_c == 0)
add++;
len = len + add;
if(len < 6)
add = add + 6 - len;
return add;
}
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;
}
In Python3 :
#!/bin/python3
import sys
numbers = "0123456789"
lower_case = "abcdefghijklmnopqrstuvwxyz"
upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
special_characters = "!@#$%^&*()-+"
def minimumNumber(n, password):
res1 = max(0, 6 - n)
res2 = 0
if not any(c in password for c in lower_case):
res2 += 1
if not any(c in password for c in upper_case):
res2 += 1
if not any(c in password for c in numbers):
res2 += 1
if not any(c in password for c in special_characters):
res2 += 1
return max(res1, res2)
if __name__ == "__main__":
n = int(input().strip())
password = input().strip()
answer = minimumNumber(n, password)
print(answer)
View More Similar Problems
Super Maximum Cost Queries
Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and
View Solution →Contacts
We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co
View Solution →No Prefix Set
There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio
View Solution →Cube Summation
You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor
View Solution →Direct Connections
Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do
View Solution →Subsequence Weighting
A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =
View Solution →