-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagrams.java
More file actions
115 lines (68 loc) · 2.59 KB
/
Anagrams.java
File metadata and controls
115 lines (68 loc) · 2.59 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Dell
*/
import java.util.*;
import java.io.*;
public class Anagrams {
static public void check(String A, String B){
HashMap<Character,Integer> hs1 = new HashMap<Character,Integer>(26);
HashMap<Character,Integer> hs2 = new HashMap<Character,Integer>(26);
for(char c : A.toCharArray()){
if(hs1.containsKey(c) == true){
int value = hs1.get(c);
value = value + 1;
hs1.put(c, value);
}
else{
hs1.put(c,1);
}
}
for(char c :B.toCharArray()){
if(hs2.containsKey(c) == true){
int value1 = hs2.get(c);
value1 = value1 + 1;
hs2.put(c, value1);
}
else{
hs2.put(c,1);
}
}
int count = 0;
for(char c : hs1.keySet()){
int difference = 0;
if(hs2.containsKey(c)){
difference = Math.abs(hs2.get(c) - hs1.get(c));
count = count + difference;
hs2.remove(c);
}
else{
count = count + hs1.get(c);
}
}
for(char ch : hs2.keySet()){
count = count + hs2.get(ch);
}
// for(Map.Entry m : hs1.entrySet()){
//
// System.out.println(m.getKey() + " -> " + m.getValue() + "\n");
// }
//
// for(Map.Entry m2 : hs2.entrySet()){
//
// System.out.println(m2.getKey() + " -> " + m2.getValue() + "\n");
// }
System.out.println("Count is ::" + count);
}
public static void main(String[] args)throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String a = br.readLine();
String b = br.readLine();
check(a,b);
}
}