We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Strings
  4. Strong Password
  5. Discussions

Strong Password

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 655 Discussions, By:

votes

Please Login in order to post a comment

  • jay_mistry456
    4 years ago+ 23 comments
    def minimumNumber(n, password):
        count = 0    
        if any(i.isdigit() for i in password)==False:
            count+=1
        if any(i.islower() for i in password)==False:
            count+=1
        if any(i.isupper() for i in password)==False:
            count+=1
        if any(i in '!@#$%^&*()-+' for i in password)==False:
            count+=1
        return max(count,6-n)
    
    183|
    Permalink
    View more Comments..
  • ilpropheta
    4 years ago+ 4 comments

    C++ folks: bitset is your friend here:

    int CharFamily(char c) 
    {
       if(c >= '0' && c <= '9') return 0;
       if(c >= 'a' && c <= 'z') return 1;
       if(c >= 'A' && c <= 'Z') return 2;
       return 3;
    }
    
    int main() 
    {
        int n; cin >> n;
        char c;
        bitset<4> mask;
    
        while (cin >> c)
        {
            mask.set(CharFamily(c));
        }
        cout << max(6-n, 4-(int)mask.count());
    }
    
    45|
    Permalink
    View more Comments..
  • quancntt05
    4 years ago+ 4 comments

    This is my Solution (Java 8)

    private static int checkPass(String s) {
    		int count = 0;
    		
    		Pattern patternDigit = Pattern.compile("(\\d)");
    		Pattern patternUpper = Pattern.compile("([A-Z])");
    		Pattern patternLower = Pattern.compile("([a-z])");
    		Pattern patternSpecial = Pattern.compile("(\\W)");
            
    		Matcher matcherDigit = patternDigit.matcher(s);
    		Matcher matcherUpper = patternUpper.matcher(s);
    		Matcher matcherLower = patternLower.matcher(s);
    		Matcher matcherSpecial = patternSpecial.matcher(s);
    		
    		if (!matcherDigit.find()) {
    			count++;
    		}
    		if (!matcherUpper.find()) {
    			count++;
    		}
    		if (!matcherLower.find()) {
    			count++;
    		}
    		if (!matcherSpecial.find()) {
    			count++;
    		}
    		if ((count+s.length())<6) {
    			count = count + 6-(count+s.length());
    		}
    		return count;
    	}
    
    27|
    Permalink
    View more Comments..
  • gartenkralle
    4 years ago+ 1 comment

    Loled hard as I saw the amount of test cases. :D

    19|
    Permalink
  • vjayendrapandey
    4 years ago+ 7 comments

    Similar logic implemented without looping, using Regular Expressions in Java.

    static int minimumNumber(int n, String password) {
            
            int lc=0,uc=0,no=0,sc=0;
            int sum=0;
            
            String lowercase = ".*[a-z]+.*";
            String uppercase = ".*[A-Z]+.*";
            String num = ".*[0-9]+.*";
            String specialchar = ".*[-!@#$%^&*()+]+.*"; 
            //Regex for pattern matching
            
            if(!(password.matches(lowercase)))
                lc++; 
            //if there is no match to a lowercase,"lc" is increased by 1.
        
            if(!(password.matches(uppercase)))
                uc++; 
            //if there is no match to an uppercase,"uc" is increased by 1.
        
            if(!(password.matches(num)))
                no++; 
            //if there is no match to a number,"no" is increased by 1.
        
            if(!(password.matches(specialchar)))
                sc++; 
            //if there is no match to a specialCharacter,"sc" is increased by 1.
            
            
            sum=lc+uc+sc+no;
            
            return (sum>(6-n))?sum:(6-n);
        }
    
    17|
    Permalink
    View more Comments..
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature