-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidAnagram.java
More file actions
49 lines (35 loc) · 1.14 KB
/
ValidAnagram.java
File metadata and controls
49 lines (35 loc) · 1.14 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
package com.vinay.practice.lc;
import java.util.HashMap;
// https://leetcode.com/problems/valid-anagram/
public class ValidAnagram {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "anagram";
String t = "nagaram";
if((s.length() != t.length()))
System.out.println("False");
HashMap<Character, Integer> countS = new HashMap<Character, Integer>();
HashMap<Character, Integer> countT = new HashMap<Character, Integer>();
for(int i=0; i<s.length(); i++){
// string s count
Character cs = s.charAt(i);
if (countS.containsKey(cs)){
countS.put(cs, countS.get(cs)+1);
} else{
countS.put(cs, 1);
}
// string t count
Character ct = t.charAt(i);
if (countT.containsKey(ct)){
countT.put(ct, countT.get(ct)+1);
} else{
countT.put(ct, 1);
}
}
if(countS.equals(countT)){
System.out.println("True");
} else {
System.out.println("False");
}
}
}