-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class Solution { | ||
Check warning on line 4 in Solved-Problems/Valid_Anagram/Main.java GitHub Actions / java-linter / java-linter
|
||
public boolean isAnagram(String s, String t) { | ||
Check warning on line 5 in Solved-Problems/Valid_Anagram/Main.java GitHub Actions / java-linter / java-linter
|
||
if(s.length() != t.length()) | ||
Check warning on line 6 in Solved-Problems/Valid_Anagram/Main.java GitHub Actions / java-linter / java-linter
Check warning on line 6 in Solved-Problems/Valid_Anagram/Main.java GitHub Actions / java-linter / java-linter
Check warning on line 6 in Solved-Problems/Valid_Anagram/Main.java GitHub Actions / java-linter / java-linter
|
||
{ | ||
Check warning on line 7 in Solved-Problems/Valid_Anagram/Main.java GitHub Actions / java-linter / java-linter
Check warning on line 7 in Solved-Problems/Valid_Anagram/Main.java GitHub Actions / java-linter / java-linter
|
||
return false; | ||
Check warning on line 8 in Solved-Problems/Valid_Anagram/Main.java GitHub Actions / java-linter / java-linter
|
||
} | ||
Check warning on line 9 in Solved-Problems/Valid_Anagram/Main.java GitHub Actions / java-linter / java-linter
|
||
|
||
Map<Character, Integer> countS = new HashMap<>(); | ||
Check warning on line 11 in Solved-Problems/Valid_Anagram/Main.java GitHub Actions / java-linter / java-linter
|
||
Map<Character, Integer> countT = new HashMap<>(); | ||
|
||
for(int i = 0;i<s.length();i++) | ||
{ | ||
char c1 = s.charAt(i); | ||
countS.put(c1,countS.getOrDefault(c1,0)+1); | ||
} | ||
|
||
for (int i = 0; i < t.length(); i++) { | ||
char c2 = t.charAt(i); | ||
countT.put(c2, countT.getOrDefault(c2, 0) + 1); | ||
} | ||
|
||
for(char c : countS.keySet()) | ||
{ | ||
if(!countT.containsKey(c) || !countT.get(c).equals(countS.get(c))) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Valid Anagram | ||
Problem Name: 242. Valid Anagram | ||
|
||
Approach: Using Hash Maps | ||
|
||
Difficulty: Easy | ||
|
||
Link: [Leetcode](https://leetcode.com/problems/valid-anagram/description/) | ||
|
||
Explanation: | ||
|
||
In this problem, we are given two strings, s and t. We need to determine if t is an anagram of s. An anagram is a word or phrase formed by rearranging the letters of another word or phrase, typically using all the original letters exactly once. | ||
|
||
Solution Explanation | ||
|
||
Check Lengths: | ||
First, check if the lengths of the two strings are equal. If not, return false immediately as they cannot be anagrams. | ||
Count Characters: | ||
|
||
Use two hash maps to count the frequency of each character in the strings s and t. | ||
Iterate through the characters of s and update the count in countS. | ||
Similarly, iterate through the characters of t and update the count in countT. | ||
Compare Counts: | ||
|
||
Compare the two hash maps. If both maps are identical, then t is an anagram of s. | ||
|
||
Examples | ||
|
||
Example 1: | ||
Input: s = "anagram", t = "nagaram" | ||
Output: true | ||
|
||
Example 2: | ||
Input: s = "rat", t = "car" | ||
Output: false | ||
|
||
Constraints | ||
1 <= s.length, t.length <= 5 * 10^4 | ||
s and t consist of lowercase English letters. |