Skip to content

Commit

Permalink
HashMap problem 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Ankush1oo8 committed Mar 15, 2024
1 parent 410abd9 commit 73c271f
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Hashing/anagram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package Hashing;
import java.util.*;
public class anagram {
public static boolean validAnagram(String s, String t){
if(s.length()!=t.length()){
return false;
}
HashMap<Character, Integer>map=new HashMap<>();

for(int i=0;i<s.length();i++){
char ch=s.charAt(i);
map.put(ch,map.getOrDefault(ch,0)+1);
}
for(int i=0;i<t.length();i++){
char ch=t.charAt(i);
if(map.get(ch)!=null){
if(map.get(ch)==1){
map.remove(ch);
}else{
map.put(ch, map.get(ch)-1);
}
}else{
return false;
}
}
return map.isEmpty();
}
public static void main(String[] args) {
String s="race";
String t="care";

System.out.println(validAnagram(s, t));

}
}

0 comments on commit 73c271f

Please sign in to comment.