From 483b81160d39288594950fbef0ab5eb6d108c2cc Mon Sep 17 00:00:00 2001 From: Hidemichi Shimura Date: Tue, 10 Jan 2023 11:25:52 -0800 Subject: [PATCH 1/2] solve 2299. Strong Password Checker II --- 2299.strong-password-checker-ii.js | 50 ++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 2299.strong-password-checker-ii.js diff --git a/2299.strong-password-checker-ii.js b/2299.strong-password-checker-ii.js new file mode 100644 index 00000000..153463ee --- /dev/null +++ b/2299.strong-password-checker-ii.js @@ -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; \ No newline at end of file From 3fd115deb3be1ba0f21a8c2bb8be09faa7f4ed7f Mon Sep 17 00:00:00 2001 From: Hidemichi Shimura Date: Tue, 10 Jan 2023 11:26:12 -0800 Subject: [PATCH 2/2] test function strongPasswordCheckerII --- strongPasswordCheckerII.test.js | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 strongPasswordCheckerII.test.js diff --git a/strongPasswordCheckerII.test.js b/strongPasswordCheckerII.test.js new file mode 100644 index 00000000..04aa1495 --- /dev/null +++ b/strongPasswordCheckerII.test.js @@ -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(); +}); \ No newline at end of file