Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1. package main;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. public class PasswordCombinations {
  9.  
  10.     private static final Map<Character, Character> replacements = new HashMap<Character, Character>() {{
  11.         put('i', '!');
  12.         put('a', '@');
  13.         put('s', '$');
  14.         put('o', '0');
  15.         put('E', '3');
  16.     }};
  17.  
  18.     /*
  19.     You are designing a New User sign-up form for a website. When a user enters text in the password field,
  20.     you want to suggest possible 'stronger' passwords to the user by swapping in special characters,
  21.     like turning an "a" into an "@". Given a password represented as a string and a character map that contains
  22.     common characters and substitutions, create a list of all possible password combinations that can be created.
  23.  
  24.     Example:
  25.     Input: Map:{{'i','!'},{'a','@'},{'s','$'},{'o','0'},{'E','3'}} Password: password
  26.  
  27.     Output:
  28.     p@ssword
  29.     p@$sword
  30.     pa$sword
  31.     p@s$word
  32.     p@$$word
  33.     pa$$word
  34.     pas$word
  35.     p@ssw0rd
  36.     p@$sw0rd
  37.     pa$sw0rd
  38.     p@s$w0rd
  39.     p@$$w0rd
  40.     pa$$w0rd
  41.     pas$w0rd
  42.     passw0rd
  43.      */
  44.     public static List<String> obfuscate(String password) {
  45.         List<String> passwords = new ArrayList<String>();
  46.  
  47.         char [] array = password.toCharArray();
  48.  
  49.         for(int i = 0; i < array.length; i++)
  50.         {
  51.             Character sub = replacements.get(array[i]);
  52.  
  53.             if(sub != null)
  54.             {
  55.                 int length = passwords.size();
  56.  
  57.                 for(int j = 0; j < length; j++)
  58.                 {
  59.                     String str = passwords.get(j);
  60.                     passwords.add(new String(str.substring(0,i)+sub+str.substring(i+1)));
  61.                 }
  62.  
  63.                 passwords.add(new String(password.substring(0,i)+sub+password.substring(i+1)));
  64.             }
  65.         }
  66.  
  67.         return passwords;
  68.     }
  69.    
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement