Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class PasswordCombinations {
- private static final Map<Character, Character> replacements = new HashMap<Character, Character>() {{
- put('i', '!');
- put('a', '@');
- put('s', '$');
- put('o', '0');
- put('E', '3');
- }};
- /*
- You are designing a New User sign-up form for a website. When a user enters text in the password field,
- you want to suggest possible 'stronger' passwords to the user by swapping in special characters,
- like turning an "a" into an "@". Given a password represented as a string and a character map that contains
- common characters and substitutions, create a list of all possible password combinations that can be created.
- Example:
- Input: Map:{{'i','!'},{'a','@'},{'s','$'},{'o','0'},{'E','3'}} Password: password
- Output:
- p@ssword
- p@$sword
- pa$sword
- p@s$word
- p@$$word
- pa$$word
- pas$word
- p@ssw0rd
- p@$sw0rd
- pa$sw0rd
- p@s$w0rd
- p@$$w0rd
- pa$$w0rd
- pas$w0rd
- passw0rd
- */
- public static List<String> obfuscate(String password) {
- List<String> passwords = new ArrayList<String>();
- char [] array = password.toCharArray();
- for(int i = 0; i < array.length; i++)
- {
- Character sub = replacements.get(array[i]);
- if(sub != null)
- {
- int length = passwords.size();
- for(int j = 0; j < length; j++)
- {
- String str = passwords.get(j);
- passwords.add(new String(str.substring(0,i)+sub+str.substring(i+1)));
- }
- passwords.add(new String(password.substring(0,i)+sub+password.substring(i+1)));
- }
- }
- return passwords;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement