- Prepare
- Algorithms
- Strings
- Strong Password
- Discussions
Strong Password
Strong Password
+ 0 comments Here is my c++ solution, the explanation is here : https://youtu.be/t0O-Knlq4lQ
char getNature(char c){ if(c >= '0' && c <='9') return 'n'; if(c >= 'a' && c <= 'z') return 'l'; if(c >= 'A' && c <= 'Z') return 'u'; return 's'; } int minimumNumber(int n, string password) { map<char, int> mp; for(char c: password){ mp[getNature(c)] = 1; } return max(4 - (int)mp.size(), 6 - n); }
+ 0 comments in Scala
def minimumNumber(n: Int, password: String): Int = { // Return the minimum number of characters to make the password strong val strTypes = immutable.Seq( (0 to 9).mkString, ('a' to 'z').mkString, ('A' to 'Z').mkString, "!@#$%^&*()-+") @tailrec def getTypeNum(ch: Char, typeIdx: Int): Int = { if(typeIdx > strTypes.length - 1) -1 if(strTypes(typeIdx).contains(ch.toString)) typeIdx else getTypeNum(ch, typeIdx + 1) } val cntNeededTypes = strTypes.length - password.toCharArray.toSet[Char].map(ch => { getTypeNum(ch, 0) }).size if(cntNeededTypes > 6 - n) cntNeededTypes else 6 - n }
+ 0 comments A JavaScript implementation:
Without comment
function minimumNumber(n, password) { const minimumLengthRequired = 6 const otherConditions = [ /[a-z]/, /[A-Z]/, /[0-9]/, /[!@#$%^&*()\-+]/, ] const conditionsMet = otherConditions.reduce((strength, condition) => { if (password.match(condition)) strength++ return strength }, 0) return Math.max( otherConditions.length - conditionsMet, minimumLengthRequired - password.length ) }
With comments
function minimumNumber(n, password) { // Condition for minumum length const minimumLengthRequired = 6 // Conditions for lowercase, uppercase, digits, and special characters const otherConditions = [ /[a-z]/, /[A-Z]/, /[0-9]/, /[!@#$%^&*()\-+]/ ] // Stores the number of conditions the password meets const conditionsMet = otherConditions.reduce((strength, condition) => { if (password.match(condition)) strength++ return strength }, 0) /** * Select the larger of from the two: * 1. The conditions that did not meet * -> Regardless of the what's the length of the password, if the conditions are not met, we need that many number of characters in the password * 2. The shortness of the length of the password * -> If all the conditions are met, we simply need the remaining number of characters. */ return Math.max( otherConditions.length - conditionsMet, minimumLengthRequired - password.length ) }
+ 0 comments include #include bool isDigit(char c) {
return c >= '0' && c <= '9'; } bool isLowercase(char c) { return c >= 'a' && c <= 'z'; } bool isUppercase(char c) { return c >= 'A' && c <= 'Z'; } bool isSpecial(char c) { char specialCharacters[] = "!@#$%^&()-+"; for (int i = 0; i < sizeof(specialCharacters) - 1; i++) { if (c == specialCharacters[i]) { return true; } } return false; } int minimumNumber(int n, char password) { int missingChars = 0; bool hasDigit = false; bool hasLowercase = false; bool hasUppercase = false; bool hasSpecial = false; for (int i = 0; i < n; i++) { char c = password[i]; if (isDigit(c)) { hasDigit = true; } else if (isLowercase(c)) { hasLowercase = true; } else if (isUppercase(c)) { hasUppercase = true; } else if (isSpecial(c)) {
hasSpecial = true; } } if (!hasDigit) { missingChars++; } if (!hasLowercase) { missingChars++; } if (!hasUppercase) { missingChars++; } if (!hasSpecial) { missingChars++; } if (n + missingChars < 6) { missingChars += 6 - (n + missingChars); } return missingChars; }
int main() { int n; scanf("%d", &n); char password[101]; scanf("%s", password); int answer = minimumNumber(n, password); printf("%d\n", answer);
return 0; }
+ 0 comments # ThinhNguyen97 """ I Miss Those Moments I Spent With You Kitchen Table Mirror Sofa """ # Python is scripting language use interpreter # import numpy as np # import matplotlib.pyplot as plt # import pandas as pd from math import * from builtins import staticmethod from collections import Counter from collections import defaultdict from collections import namedtuple from collections import deque from queue import LifoQueue import heapq import functools import hashlib from datetime import datetime, timedelta import json import re from itertools import * import queue from bisect import bisect_left def solve(n, password): numbers = "0123456789" lower_case = "abcdefghijklmnopqrstuvwxyz" upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" special_characters = "!@#$%^&*()-+" # Initialize flags to check if the password contains required characters has_digit = False has_lower = False has_upper = False has_special = False # Check each character in the password for char in password: if char in numbers: has_digit = True elif char in lower_case: has_lower = True elif char in upper_case: has_upper = True elif char in special_characters: has_special = True # Calculate the number of characters needed to make the password strong characters_needed = 0 if not has_digit: characters_needed += 1 if not has_lower: characters_needed += 1 if not has_upper: characters_needed += 1 if not has_special: characters_needed += 1 # Ensure the password length is at least 6 if n + characters_needed < 6: characters_needed += 6 - (n + characters_needed) return characters_needed def main(): n = int(input().strip()) password = input() ans = solve(n, password) print(ans) if __name__ == '__main__': main()
Sort 817 Discussions, By:
Please Login in order to post a comment