Untitled

mail@pastecode.io avatar
unknown
java
a year ago
1.1 kB
11
Indexable
Never
import java.util.*;

public class PseudoEquivalentStrings {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        scanner.nextLine(); // Consume the newline character
        
        String w1 = scanner.next();
        String w2 = scanner.next();
        
        if (arePseudoEquivalent(w1, w2)) {
            String result = (w1.compareTo(w2) < 0) ? w1 : w2;
            System.out.println(result);
        } else {
            System.out.println("-1");
        }
    }
    
    public static boolean arePseudoEquivalent(String w1, String w2) {
        int[] count1 = new int[26];
        int[] count2 = new int[26];
        
        for (char c : w1.toCharArray()) {
            count1[c - 'a']++;
        }
        
        for (char c : w2.toCharArray()) {
            count2[c - 'a']++;
        }
        
        for (int i = 0; i < 26; i++) {
            if (Math.abs(count1[i] - count2[i]) > 3) {
                return false;
            }
        }
        
        return true;
    }
}