Skip to content

Commit

Permalink
Create Valid_anagram.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahesh-addagatla authored Apr 21, 2024
1 parent d9a433f commit b3e9098
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Valid_anagram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public boolean isAnagram(String s, String t) {
int n=s.length();
if(t.length()!=n)return false;
HashMap<Character,Integer> hs=new HashMap<>();
for(int i=0;i<n;i++){
char ch=s.charAt(i);
if(!hs.containsKey(ch)){
hs.put(ch,1);
}
else{
hs.put(ch,hs.get(ch)+1);
}
}
for(int i=0;i<n;i++){
char ch=t.charAt(i);
if(!hs.containsKey(ch))return false;
else{
hs.put(ch,hs.get(ch)-1);
}
}
for(Map.Entry<Character,Integer> map : hs.entrySet()){
int i=map.getValue();
if(i>0)return false;
}
return true;
}
}

0 comments on commit b3e9098

Please sign in to comment.