Yargi

MirrorWords

Aug 13th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class MirrorWords {
  8.     public static void main(String[] args) {
  9.         Scanner sc = new Scanner(System.in);
  10.         String str = sc.nextLine();
  11.         sc.close();
  12.         int pairCount = 0;
  13.         List<String> mirrors = new ArrayList<>();
  14.  
  15.         Pattern regex = Pattern.compile("([@]|[#])([A-Za-z]{3,})(\\1{2})([A-Za-z]{3,})(\\1)");
  16.         Matcher matcher = regex.matcher(str);
  17.  
  18.         while (matcher.find()){
  19.  
  20.             pairCount++;
  21.             String raw = matcher.group();
  22.  
  23.             String[] toCheck = raw.substring(1, raw.length() - 1).split(raw.charAt(0) + "+");
  24.             if (toCheck[0].equals(new StringBuilder(toCheck[1]).reverse().toString())){
  25.                 mirrors.add(String.format("%s <=> %s", toCheck[0], toCheck[1]));
  26.             }
  27.         }
  28.         if (pairCount > 0) {
  29.             System.out.println(pairCount + " word pairs found!");
  30.         }
  31.         else {
  32.             System.out.println("No word pairs found!");
  33.         }
  34.         if (!mirrors.isEmpty()) {
  35.             System.out.println("The mirror words are:");
  36.             System.out.println(String.join(", ", mirrors));
  37.         }
  38.         else {
  39.             System.out.println("No mirror words!");
  40.         }
  41.  
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment