Strong Password

Sort by

recency

|

909 Discussions

|

  • + 0 comments

    Perl solution:

    sub minimumNumber {
        my ($n, $str) = @_;
        my $cnt = 0;
        $cnt++ if ($str !~ m/[a-z]/m);
        $cnt++ if ($str !~ m/[A-Z]/m);
        $cnt++ if ($str !~ m/[\!\@\#\$\%\^\&\*\(\)\-\+]/m);
        $cnt++ if ($str !~ m/\d+/m);
        if ($n < 6) {
            if ((length($str) + $cnt) < 6) {
                $cnt = 6 - length($str);
            }        
        }
        
        return $cnt;
    
    }
    
  • + 0 comments

    Python solution

    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 = "!@#$%^&*()-+"
        
        not_spl = 1
        not_lower = 1
        not_upper = 1
        not_num = 1
        
        for i in range(n):
            if not_num and password[i] in numbers:
                not_num = 0
            elif not_lower and password[i] in lower_case:
                not_lower = 0
            elif not_upper and password[i] in upper_case:
                not_upper = 0
            elif not_spl and password[i] in special_characters:
                not_spl = 0
        
        req_char = not_lower + not_num + not_spl + not_upper
        
        if n<6:
            return max(req_char, 6 - n)
        else:
            return req_char
    
  • + 0 comments

    My answer in Typescript, simple, not minimized

    function minimumNumber(n: number, password: string): number {
        /**
         * simple
         * 1. check [password] missing [x] in length
         * 2. check [password] missing [y] in types of character
         * 3. return the larger [x] or [y]
         * 
         * why? 
         * if you miss 3 length but miss 1 type, you can write that missing type
         * in 3 character missing length to satisfy both conditions. 
         * if tou miss 1 length but miss 3 type, you need to type at least 3 character
         * in 3 type and that satisfy missing length condition too.
         */
        const check_miss_length = (): number => {
            if (n < 6) return 6 - n
            return 0
        }
        const check_miss_types = (): number => {
            const type_digit = (/([\d])/.test(password)) ? 0 : 1
            const type_lower = (/([a-z])/.test(password)) ? 0 : 1
            const type_upper = (/([A-Z])/.test(password)) ? 0 : 1
            const type_speci = (/([^a-z0-9A-Z])/.test(password)) ? 0 : 1
            return type_digit + type_lower + type_upper + type_speci;
        }
    
        return Math.max(check_miss_length(), check_miss_types())
    }
    
  • + 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
    def minimumNumber(n):
        low=1
        up=1
        di=1
        spl=1
    
        for i in n:
            if i.islower():
                low=0
            elif i.isupper():
                up=0
            elif i.isdigit():
                di=0
            elif i in '!@#$%^&*()-+':
                spl=0
        ans=low+up+di+spl
        if len(n)+ans<6:
            ans+=6-(len(n)+ans)
            
        return ans