forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
119 lines (94 loc) · 3.44 KB
/
Solution.java
File metadata and controls
119 lines (94 loc) · 3.44 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
116
117
118
119
package strings;
//Two strings and are called anagrams if they consist same characters, but may be in different orders. So the list of anagrams of is .
//
//Given two strings, print Anagrams if they are anagrams, print Not Anagrams if they are not. The strings may consist at most English characters; the comparison should NOT be case sensitive.
//
//This exercise will verify that you can sort the characters of a string, or compare frequencies of characters.
//
//
//
//Sample Input 0
//
//anagram
//margana
//Sample Output 0
//
//Anagrams
//Sample Input 1
//
//anagramm
//marganaa
//Sample Output 1:
//
//Not Anagrams
import java.io.*;
import java.util.*;
public class Solution {
//My hash map is initialized based on the fact that this is only using English characters as is stated in the problem constraints
//Overall time complexity: O(n)
//Overall space complexity: O(1)
static boolean isAnagram(String a, String b)
{
boolean anagram = true;
Character[] charSet = {'A','B','C','D','E',
'F','G','H','I','J',
'K','L','M','N','O',
'P','Q','R','S','T',
'U','V','W','X','Y','Z'};
HashMap<Character,Integer> ALetterFrequency = new HashMap<Character,Integer>();
HashMap<Character,Integer> BLetterFrequency = new HashMap<Character,Integer>();
intializeHash(ALetterFrequency, charSet); //O(n)
intializeHash(BLetterFrequency, charSet); //O(n)
a = a.toUpperCase(); //O(n)
b = b.toUpperCase(); //O(n)
//Originally I took a different approach to this problem because I was using substring and it
// runs in O(n) which would make my algorithm run in O(n^2) with constant space, but then I
// remembered that charAt() runs in constant time so utilizing that function I was able to
// have a time complexity of O(n) and space complexity of O(1)
for(int i = 0;i <a.length();i++) //O(n)
{
Character letter = a.charAt(i); //O(1)
Integer frequency = ALetterFrequency.get(letter);
ALetterFrequency.put(letter,++frequency);
}
for(int i = 0;i <b.length();i++) //O(n)
{
Character letter = b.charAt(i); //O(1)
Integer frequency = BLetterFrequency.get(letter);
BLetterFrequency.put(letter,++frequency);
}
//Compare Hash
for(Character letter : charSet)
{
if(!ALetterFrequency.get(letter).equals(BLetterFrequency.get(letter)))
{
anagram = false;
}
}
return anagram;
}
static void intializeHash(HashMap hash, Character[] set)
{
for(Character letter : set)
{
hash.put(letter,0);
}
}
static Character[] toCharacterArray(char[] cArray)
{
Character[] CArray = new Character[cArray.length];
for(int i =0; i<cArray.length;i++)
{
CArray[i] = (Character) cArray[i];
}
return CArray;
}
public static void main(String[] args) {
//Scanner scan = new Scanner(System.in);
String a = "anagramm";
String b = "marganaa";
//scan.close();
boolean ret = isAnagram(a, b);
System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
}
}