DEV Community

Abhishek Chaudhary
Abhishek Chaudhary

Posted on

2 1

Strong Password Checker II

A password is said to be strong if it satisfies all the following criteria:

  • It has at least 8 characters.
  • It contains at least one lowercase letter.
  • It contains at least one uppercase letter.
  • It contains at least one digit.
  • It contains at least one special character. The special characters are the characters in the following string: "!@#$%^&*()-+".
  • It does not contain 2 of the same character in adjacent positions (i.e., "aab" violates this condition, but "aba" does not).

Given a string password, return true if it is a strong password. Otherwise, return false.

Example 1:

Input: password = "IloveLe3tcode!"
Output: true
Explanation: The password meets all the requirements. Therefore, we return true.

Example 2:

Input: password = "Me+You--IsMyDream"
Output: false
Explanation: The password does not contain a digit and also contains 2 of the same character in adjacent positions. Therefore, we return false.

Example 3:

Input: password = "1aB!"
Output: false
Explanation: The password does not meet the length requirement. Therefore, we return false.

Constraints:

  • 1 <= password.length <= 100
  • password consists of letters, digits, and special characters: "!@#$%^&*()-+".

SOLUTION:

class Solution:
    def strongPasswordCheckerII(self, p: str) -> bool:
        n = len(p)
        if len(p) < 8:
            return False
        lc = 0
        uc = 0
        dg = 0
        sc = 0
        for i in range(n):
            if p[i].islower():
                lc += 1
            if p[i].isupper():
                uc += 1
            if p[i].isdigit():
                dg += 1
            if p[i] in "!@#$%^&*()-+":
                sc += 1
            if i < n - 1 and p[i] == p[i + 1]:
                return False
        return lc >= 1 and uc >= 1 and dg >= 1 and sc >= 1
Enter fullscreen mode Exit fullscreen mode

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay