Advertisement
Guest User

Анаграми

a guest
Jan 15th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1.  
  2.  
  3. import java.io.InputStream;
  4. import java.util.*;
  5.  
  6. public class Anagrams {
  7.  
  8.     public static void main(String[] args) {
  9.         findAll(System.in);
  10.     }
  11.  
  12.     public static void findAll(InputStream inputStream) {
  13.  
  14.  
  15.         Scanner scanner = new Scanner(inputStream);
  16.         Map<String, List<String>> mapa = new TreeMap<>();
  17.         Map<String, String> keyTokey = new TreeMap<>();
  18.  
  19.  
  20.         while (scanner.hasNext()) {
  21.  
  22.  
  23.             String zbor = scanner.nextLine();
  24.  
  25.             if(keyTokey.containsKey(sortedString(zbor))){
  26.                 mapa.get(keyTokey.get(sortedString(zbor))).add(zbor);
  27.             }else{
  28.                 List<String> list = new LinkedList<>();
  29.                 list.add(zbor);
  30.                 mapa.put(zbor, list);
  31.                 keyTokey.put(sortedString(zbor), zbor);
  32.             }
  33.  
  34.         }
  35.         scanner.close();
  36.  
  37.  
  38.         for (String key : mapa.keySet()) {
  39.             if(mapa.get(key).size()>4)
  40.             System.out.println(String.join(" ", mapa.get(key)));
  41.         }
  42.  
  43.     }
  44.  
  45.     public static String removeWhitespaces(String word) {
  46.         return word.replaceAll("\\s+", "");
  47.     }
  48.  
  49.     public static String sortedString(String s){
  50.         s = removeWhitespaces(s);
  51.         char[] c = s.toCharArray();
  52.         Arrays.sort(c);
  53.         return new String(c);
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement