Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Scanner;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. import static java.util.stream.Collectors.joining;
  7.  
  8. public class Demo {
  9.     public static void main(String[] args) {
  10.  
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.      String messege = scanner.nextLine();
  14.         Pattern pattern = Pattern.compile("(@|#)(?<first>[A-Za-z]{3,})(\\1){2}(?<second>[A-Za-z]{3,})(\\1)");
  15.         Matcher matcher = pattern.matcher(messege);
  16.         LinkedHashMap<String,String> mirror = new LinkedHashMap<>();
  17.  
  18.         int pairCount = 0;
  19.  
  20.         while(matcher.find()){
  21.             pairCount++;
  22.             String first = matcher.group("first");
  23.             String second = matcher.group("second");
  24.             StringBuilder reverse = new StringBuilder(first);
  25.             String compare = reverse.reverse().toString();
  26.  
  27.             if (compare.equals(second)){
  28.                 mirror.put(first, second);
  29.             }
  30.         }
  31.  
  32.         if (pairCount > 0){
  33.             System.out.printf("%d word pairs found!%n", pairCount);
  34.         }else {
  35.             System.out.println("No word pairs found!");
  36.         }
  37.         if (mirror.size() > 0){
  38.             System.out.println("The mirror words are:");
  39.  
  40.             String s = mirror.entrySet()
  41.                     .stream()
  42.                     .map(e -> e.getKey()+" <=> "+ e.getValue())
  43.                     .collect(joining(", "));
  44.             System.out.println(s);
  45.         }else {
  46.             System.out.println("No mirror words!");
  47.         }
  48.     }
  49.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement