-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCryptarithmatic.java
88 lines (80 loc) · 2.21 KB
/
Cryptarithmatic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package Cryptarithmatic;
import java.util.*;
public class Cryptarithmatic {
public static int getNumber(String s,HashMap<Character,Integer>hm)
{
String num="";
for(int i=0;i<s.length();i++)
{
num+=hm.get(s.charAt(i));
}
return Integer.parseInt(num);
}
public static void solve(int i,String unique,HashMap<Character,Integer>hm,boolean[]used,String[]words,String result)
{
if(i==unique.length())
{
int sum=0;
for(String s:words)
{
sum+=getNumber(s,hm);
}
int r=getNumber(result,hm);
if(sum==r)
{
for(int k=0;k<=25;k++)
{
char ch=(char)('A'+k);
if(hm.containsKey(ch))
{
System.out.print(ch+" -> "+hm.get(ch)+" ");
}
}
System.out.println();
}
return;
}
char ch=unique.charAt(i);
for(int j=0;j<=9;j++)
{
if(!used[j])
{
hm.put(ch,j);
used[j]=true;
solve(i+1,unique,hm,used,words,result);
hm.put(ch,-1);
used[j]=false;
}
}
}
public static void main(String[]args)
{
String[]words={"SEND","MORE"};
String result="MONEY";
HashMap<Character,Integer>hm=new HashMap<>();
String unique="";
for(String s:words)
{
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(!hm.containsKey(ch))
{
hm.put(ch,-1);
unique+=ch;
}
}
}
for(int i=0;i<result.length();i++)
{
char ch=result.charAt(i);
if(!hm.containsKey(ch))
{
hm.put(ch,-1);
unique+=ch;
}
}
boolean []used=new boolean[10];
solve(0,unique,hm,used,words,result);
}
}