Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions 2299.strong-password-checker-ii.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* URL of this problem
* https://leetcode.com/problems/strong-password-checker-ii/description/
*
* @param {string} password
* @return {boolean}
*/

var strongPasswordCheckerII = function(password) {
let HasLowerLetter = false
let HasUpperLetter = false;
let HasOneDigit = false;
let HasSpecialChar = false;

if (password.length < 8) {
return false;
};

for (let i = 0; i < password.length; i++) {
const PasswordChar = password.charAt(i);

// Check if the character is a LOWER English letter from UTF-16 code
// The range of English lower letter in UTF-16 is [97,122]
if (PasswordChar.charCodeAt(0) >= 97 && PasswordChar.charCodeAt(0) <= 122) {
HasLowerLetter = true;
}
// Check if the character is a UPPER English letter from UTF-16 code
// The range of English lower letter in UTF-16 is [65,90]
else if (PasswordChar.charCodeAt(0) >= 65 && PasswordChar.charCodeAt(0) <= 90) {
HasUpperLetter = true;
} else if (!isNaN(Number(PasswordChar))) {
HasOneDigit = true;
} else if ("!@#$%^&*()-+".includes(PasswordChar)) {
HasSpecialChar = true;
}

// Return false if there are the same characters adjacent to each other
if (PasswordChar === password.charAt(i + 1)) {
return false;
}
}

return (
HasLowerLetter &&
HasUpperLetter &&
HasOneDigit &&
HasSpecialChar
);
};

module.exports = strongPasswordCheckerII;
39 changes: 39 additions & 0 deletions strongPasswordCheckerII.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* Strong password 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).
*/

const strongPasswordCheckerII = require("./2299.strong-password-checker-ii");

test("Return true if the password is strong", () => {
expect(strongPasswordCheckerII("IloveLe3tcode!")).toBeTruthy();
});

test("Return false if the password length is less than 8", () => {
expect(strongPasswordCheckerII("Ilove")).toBeFalsy();
});

test("Return false if the password contains NO lowercase letter", () => {
expect(strongPasswordCheckerII("IlOVELE3TCODE")).toBeFalsy();
});

test("Return false if the password contains NO uppercase letter", () => {
expect(strongPasswordCheckerII("ilovele3tcode!")).toBeFalsy();
});

test("Return false if the password contains NO digit", () => {
expect(strongPasswordCheckerII("iloveLetcode!")).toBeFalsy();
});

test("Return false if the password contains NO special character", () => {
expect(strongPasswordCheckerII("iloveLe3tcode")).toBeFalsy();
});

test("Return false if the password contains the same characters adjacent to each other", () => {
expect(strongPasswordCheckerII("illoveLe3tcode")).toBeFalsy();
});