-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2554 from itshagunrathore/validanagramhash
Validanagramhash
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
*.iml | ||
.git/objects/ | ||
.DS_Store | ||
*.idea* |
55 changes: 55 additions & 0 deletions
55
Program's_Contributed_By_Contributors/Java_Programs/Data_structure/AnagramTechniques.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package Arrays; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class AnagramTechniques { | ||
|
||
|
||
|
||
public static boolean isAnnagramUsingFrequencyCounter(String s, String t) { | ||
// You can use py counters -- return Counter(s) == Counter(t) | ||
|
||
} | ||
|
||
public static boolean isAnagramIUsingHash(String s, String t) { | ||
|
||
if (s.length() != t.length()) return false; | ||
// You can use py counters -- return Counter(s) == Counter(t) | ||
|
||
// Use hashing | ||
Map<String, Integer> smap = new HashMap(); | ||
Map<String, Integer> tmap = new HashMap(); | ||
|
||
String sArray[] = s.split(""); | ||
String tArray[] = t.split(""); | ||
|
||
for (int i = 0; i < sArray.length; i++) { | ||
|
||
if (smap.containsKey(sArray[i])) { | ||
smap.put(sArray[i], smap.get(sArray[i]) + 1); | ||
} else { | ||
smap.put(sArray[i], 1); | ||
} | ||
|
||
if (tmap.containsKey(tArray[i])) { | ||
tmap.put(tArray[i], tmap.get(tArray[i]) + 1); | ||
} else { | ||
tmap.put(tArray[i], 1); | ||
} | ||
} | ||
|
||
for (String ch : smap.keySet()) { | ||
if (smap.get(ch) != tmap.get(ch)) return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static void main(String[] args) { | ||
String s = "anagramz", t = "managraq"; | ||
System.out.println(isAnagramIUsingHash(s, t)); | ||
} | ||
} | ||
|