Advertisement
Guest User

Untitled

a guest
Sep 8th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. import java.util.Hashtable;
  2.  
  3. public class GoogleChallenge3 {
  4.  
  5.     public int solution(String[] L) {
  6.         Hashtable<String, Integer> map = new Hashtable<>();
  7.         for (int i = 0; i < L.length; i++) {
  8.             String key = getDomain(L[i]) + countLocalName(L[i], 0);
  9.             if (!map.containsKey(key))
  10.                 map.put(key, 1);
  11.             else
  12.                 map.put(key, map.get(key) + 1);
  13.         }
  14.         int result = 0;
  15.         for (String s : map.keySet()) {
  16.             if (map.get(s) > 1)
  17.                 result++;
  18.         }
  19.         return result;
  20.     }
  21.  
  22.     private int countLocalName(String email, int i) {
  23.         if (email.charAt(i) == '@' || email.charAt(i) == '+')
  24.             return 0;
  25.         if (email.charAt(i) == '.')
  26.             return countLocalName(email, i + 1);
  27.         return countLocalName(email, i + 1) + 1;
  28.     }
  29.  
  30.     private String getDomain(String email) {
  31.         return email.substring(email.indexOf('@'), email.length());
  32.     }
  33.  
  34.     public static void main(String[] args) {
  35.         int N = 7;
  36.         String[] L = new String[N];
  37.         L[0] = "a.b@example.com";
  38.         L[1] = "x@example.com";
  39.         L[2] = "x@exa.mple.com";
  40.         L[3] = "ab+1@example.com";
  41.         L[4] = "y@example.com";
  42.         L[5] = "y@example.com";
  43.         L[6] = "y@example.com";
  44.  
  45.         System.out.println(new GoogleChallenge3().solution(L));
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement