Problem overview
Some passwords are weak whatever their characters look like, because everyone tries them first. This version rejects those. The program starts with a Set<String> named common holding four well-known passwords (password, 123456, qwerty and letmein) and a String password to check.
It prints the password, prints whether it is in the set, and then prints a verdict: Weak: reject for a common password and Strong: accept otherwise. For qwerty:
Password: qwerty
Common: True
Weak: reject
For Tr0ub4dor, which is not in the set:
Password: Tr0ub4dor
Common: False
Strong: accept
Approach: ask a set
The question is "is this exact text one of the known passwords?" A HashSet answers it directly. It stores each value once and finds a value by its hash code, so contains takes constant time on average, whether the set holds four passwords or four million. A real list of common passwords can be very long, and this is why a set is the right structure. Looping over a list with equals would work but takes time proportional to the list's size.
The program does not need to search for a position or count anything: it needs one boolean, and the verdict follows from it. Storing the answer in a variable, isCommon, lets the program use it twice, once for the printed line and once for the decision.
Step by step
- Print the password. The line is
Password:joined topasswordwith+. - Look it up.
boolean isCommon = common.contains(password);istruewhen the set holds that exact text. - Print the answer. The text
Common:followed byTrueorFalse. Java prints abooleanin lowercase, so the program picks the text withisCommon ? "True" : "False". - Branch on the result. An
if (isCommon)printsWeak: reject. - Add the
else. When the password is not common it printsStrong: accept.
The solution in Java
Set<String> common = new HashSet<>(Arrays.asList("password", "123456", "qwerty", "letmein"));
String password = "qwerty";
System.out.println("Password: " + password);
boolean isCommon = common.contains(password);
System.out.println("Common: " + (isCommon ? "True" : "False"));
if (isCommon) {
System.out.println("Weak: reject");
} else {
System.out.println("Strong: accept");
}
The Java details worth a closer look:
- Building the set.
Arrays.asList(...)turns the four strings into a fixed-size list, andnew HashSet<>(...)copies them into a set. The variable has the interface typeSet<String>, which is the usual way to declare it. - Imports.
Set,HashSetandArraysare injava.util, so a standalone file needsimport java.util.*;. containsusesequals. Strings are objects, and comparing them with==checks whether they are the same object, not whether they have the same characters.new String("qwerty") == "qwerty"isfalse, whileequalsgivestrue. A set relies onhashCodeandequals, so aStringbuilt at run time is found just as well as a literal.- The set has no order. A
HashSetdoes not remember insertion order. The program never iterates over it, so the order does not matter here. - The boolean as text. Without the conditional operator,
"Common: " + isCommonprintsCommon: true. The parentheses around the conditional are required, because+binds tighter than? :.
Complexity
Time: one contains call, which is O(1) on average for the lookup plus the time to hash the password, proportional to its length. Space: O(m) for a set of m common passwords, which the program builds once.
Edge cases and common mistakes
- Case matters.
Qwertyis not the same text asqwerty, so it is not found: the output isCommon: FalseandStrong: accept. Lowercasing the password before the lookup, withpassword.toLowerCase(), would catch such variants, but this lesson does an exact match. - Not common does not mean strong. The verdict says
Strong: acceptfor any password missing from the set, even a very short one. This version only checks the list. - Whitespace. A password with a trailing space, such as
"qwerty ", is a different string and is not found. - Deciding with
==. Comparing a password to a set element with==checks references. Usecontainsorequals. - Printing before looking up. The order of the output is fixed: the password line first, then
Common, then the verdict. ComputeisCommonbefore printing the second line. - Using a
Listfor the lookup.list.containsgives the same answers but scans the list from the start, so it slows down as the list grows.
Testing
qwertygivesPassword: qwerty,Common: TrueandWeak: reject.Tr0ub4dorgivesPassword: Tr0ub4dor,Common: FalseandStrong: accept.QwertygivesPassword: Qwerty,Common: FalseandStrong: accept.
