In this HackerRank Strong Password problem, Give the string she typed, can you find the minimum number of characters she must add to make her password strong.
Problem solution in Python programming.
#!/bin/python3 import sys def minimumNumber(n, password): numbers = "0123456789" lower_case = "abcdefghijklmnopqrstuvwxyz" upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" special_characters = "!@#$%^&*()-+" n_bool = 1 l_bool = 1 u_bool = 1 s_bool = 1 for c in password: if c in numbers: n_bool = 0 elif c in lower_case: l_bool = 0 elif c in upper_case: u_bool = 0 elif c in special_characters: s_bool = 0 return max(6-n, n_bool + l_bool + u_bool + s_bool) if __name__ == "__main__": n = int(input().strip()) password = input().strip() answer = minimumNumber(n, password) print(answer)
Problem solution in Java Programming.
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 password) { boolean lowercase = false; boolean uppercase = false; boolean number = false; boolean special = false; char[] schars = "!@#$%^&*()-+".toCharArray(); Set<Character> cs = new HashSet<>(); for (char c : schars) { cs.add(c); } for (int i = 0; i < n; i++) { char c = password.charAt(i); if (c >= '0' && c <= '9') number = true; if (c >= 'a' && c <= 'z') lowercase = true; if (c >= 'A' && c <= 'Z') uppercase = true; if (cs.contains(c)) special = true; } int need = 0; need += lowercase ? 0 : 1; need += uppercase ? 0 : 1; need += number ? 0 : 1; need += special ? 0 : 1; return n + need < 6 ? 6 - n : need; } 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(); } }
Problem solution in C++ programming.
#include <bits/stdc++.h> using namespace std; int n, a, b, c, d; string s; int main() { cin >> n >> s; for (int i = 0; i < n; i++) { if (s[i] >= '0' && s[i] <= '9') a = 1; else if (s[i] >= 'a' && s[i] <= 'z') b = 1; else if (s[i] >= 'A' && s[i] <= 'Z') c = 1; else d = 1; } cout << max(6 - n, 4 - a - b - c - d); return 0; }
Problem solution in C programming.
#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) { char* numbers = "0123456789"; char* lower_case = "abcdefghijklmnopqrstuvwxyz"; char* upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char* special_characters = "!@#$%^&*()-+"; int num = 1; int lc = 1; int uc = 1; int sc = 1; for (int i = 0; i < n; i++) { if (strchr(numbers, password[i])) num = 0; if (strchr(lower_case, password[i])) lc = 0; if (strchr(upper_case, password[i])) uc = 0; if (strchr(special_characters, password[i])) sc = 0; } int s = (num + lc + uc + sc + n < 6) ? 6 - (num + lc + uc + sc + n) : 0; return (num + lc + uc + sc + s); // Return the minimum number of characters to make the password strong } int main() { int n; scanf("%i", &n); char* password = (char *)malloc(512000 * sizeof(char)); scanf("%s", password); int answer = minimumNumber(n, password); printf("%dn", answer); return 0; }
Problem solution in JavaScript programming.
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 var numbers = "0123456789".split('') var lower_case = "abcdefghijklmnopqrstuvwxyz".split('') var upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('') var special_characters = "!@#$%^&*()-+".split('') var count = 0; var find = { n: false, l: false, u: false, s: false } for(var i = 0; i < n; i ++) { var c = password.charAt(i); if(!find.n && findChar(numbers, c)) { count ++; find.n = true; } if(!find.l && findChar(lower_case, c)) { count ++; find.l = true; } if(!find.u && findChar(upper_case, c)) { count ++; find.u = true; } if(!find.s && findChar(special_characters, c)) { count ++; find.s = true; } } count = 4 - count; if(n + count < 6) { count = 6 - n; } function findChar(arr, c) { var l = arr.length; for(var i = 0; i < l; i ++) { if(c == arr[i]) return true; } return false; } return count; } function main() { var n = parseInt(readLine()); var password = readLine(); var answer = minimumNumber(n, password); process.stdout.write("" + answer + "n"); }