Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.InputStream;
- import java.util.*;
- public class Anagrams {
- public static void main(String[] args) {
- findAll(System.in);
- }
- public static void findAll(InputStream inputStream) {
- Scanner scanner = new Scanner(inputStream);
- Map<String, List<String>> mapa = new TreeMap<>();
- Map<String, String> keyTokey = new TreeMap<>();
- while (scanner.hasNext()) {
- String zbor = scanner.nextLine();
- if(keyTokey.containsKey(sortedString(zbor))){
- mapa.get(keyTokey.get(sortedString(zbor))).add(zbor);
- }else{
- List<String> list = new LinkedList<>();
- list.add(zbor);
- mapa.put(zbor, list);
- keyTokey.put(sortedString(zbor), zbor);
- }
- }
- scanner.close();
- for (String key : mapa.keySet()) {
- if(mapa.get(key).size()>4)
- System.out.println(String.join(" ", mapa.get(key)));
- }
- }
- public static String removeWhitespaces(String word) {
- return word.replaceAll("\\s+", "");
- }
- public static String sortedString(String s){
- s = removeWhitespaces(s);
- char[] c = s.toCharArray();
- Arrays.sort(c);
- return new String(c);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement