Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class MirrorWords {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String str = sc.nextLine();
- sc.close();
- int pairCount = 0;
- List<String> mirrors = new ArrayList<>();
- Pattern regex = Pattern.compile("([@]|[#])([A-Za-z]{3,})(\\1{2})([A-Za-z]{3,})(\\1)");
- Matcher matcher = regex.matcher(str);
- while (matcher.find()){
- pairCount++;
- String raw = matcher.group();
- String[] toCheck = raw.substring(1, raw.length() - 1).split(raw.charAt(0) + "+");
- if (toCheck[0].equals(new StringBuilder(toCheck[1]).reverse().toString())){
- mirrors.add(String.format("%s <=> %s", toCheck[0], toCheck[1]));
- }
- }
- if (pairCount > 0) {
- System.out.println(pairCount + " word pairs found!");
- }
- else {
- System.out.println("No word pairs found!");
- }
- if (!mirrors.isEmpty()) {
- System.out.println("The mirror words are:");
- System.out.println(String.join(", ", mirrors));
- }
- else {
- System.out.println("No mirror words!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment