import java.util.*;
class GFG {
// Function to check if the puzzle is solvable with the given character assignments
static boolean solvePuzzleHelper(String a, String b, String sum, int pos, int carry,
Map<Character, Integer> charToDigit, boolean[] usedDigits) {
// Base case: if we have processed all digits
if (pos >= sum.length()) {
return carry == 0;
}
// Calculate sum at current position
int sumVal = carry;
if (pos < a.length() && charToDigit.containsKey(a.charAt(a.length() - 1 - pos))) {
sumVal += charToDigit.get(a.charAt(a.length() - 1 - pos));
}
if (pos < b.length() && charToDigit.containsKey(b.charAt(b.length() - 1 - pos))) {
sumVal += charToDigit.get(b.charAt(b.length() - 1 - pos));
}
char sumChar = sum.charAt(sum.length() - 1 - pos);
// If sumChar is already assigned, check if it matches
if (charToDigit.containsKey(sumChar)) {
if (charToDigit.get(sumChar) != sumVal % 10) {
return false;
}
return solvePuzzleHelper(a, b, sum, pos + 1, sumVal / 10, charToDigit, usedDigits);
}
// Ensure digit is not already used
if (usedDigits[sumVal % 10]) {
return false;
}
// Assign the digit to sumChar
charToDigit.put(sumChar, sumVal % 10);
usedDigits[sumVal % 10] = true;
// Recur for next position
if (solvePuzzleHelper(a, b, sum, pos + 1, sumVal / 10, charToDigit, usedDigits)) {
return true;
}
// Backtrack if assignment fails
charToDigit.remove(sumChar);
usedDigits[sumVal % 10] = false;
return false;
}
// Function to assign digits to unique characters and check if assignment is valid
static boolean assignDigits(String a, String b, String sum, int index,
String order, Map<Character, Integer> charToDigit, boolean[] usedDigits) {
// Base case: If all characters are assigned
if (index == order.length()) {
return solvePuzzleHelper(a, b, sum, 0, 0, charToDigit, usedDigits);
}
char ch = order.charAt(index);
// Try assigning each digit to the character
for (int digit = 0; digit < 10; digit++) {
if (!usedDigits[digit]) {
charToDigit.put(ch, digit);
usedDigits[digit] = true;
if (assignDigits(a, b, sum, index + 1, order, charToDigit, usedDigits)) {
return true;
}
// Backtrack if unsuccessful
usedDigits[digit] = false;
charToDigit.remove(ch);
}
}
return false;
}
// Main function to solve Cryptarithmetic puzzle
static List<String> solvePuzzle(String a, String b, String sum) {
Map<Character, Integer> charToDigit = new HashMap<>();
boolean[] usedDigits = new boolean[10];
Set<Character> uniqueChars = new HashSet<>();
StringBuilder order = new StringBuilder();
// Identify unique characters in the input strings
for (char ch : (a + b + sum).toCharArray()) {
if (uniqueChars.add(ch)) {
order.append(ch);
}
}
// Assign digits to characters and check validity
if (assignDigits(a, b, sum, 0, order.toString(), charToDigit, usedDigits)) {
StringBuilder x = new StringBuilder(), y = new StringBuilder(), z = new StringBuilder();
for (char ch : a.toCharArray()) x.append(charToDigit.get(ch));
for (char ch : b.toCharArray()) y.append(charToDigit.get(ch));
for (char ch : sum.toCharArray()) z.append(charToDigit.get(ch));
return Arrays.asList(x.toString(), y.toString(), z.toString());
}
return Collections.singletonList("-1");
}
public static void main(String[] args) {
String a = "send", b = "more", sum = "money";
List<String> ans = solvePuzzle(a, b, sum);
for (String res : ans) {
System.out.print(res + " ");
}
}
}