Skip to content

Commit

Permalink
feat: Implement solution for 242. Valid Anagram and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasrajiv327 committed Jul 25, 2024
1 parent a326a8c commit 9411d2e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Solved-Problems/Valid_Anagram/Main.java
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

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 The name of the outer type and the file do not match. Raw Output: /github/workspace/./Solved-Problems/Valid_Anagram/Main.java:4:1: warning: The name of the outer type and the file do not match. (com.puppycrawl.tools.checkstyle.checks.OuterTypeFilenameCheck)
public boolean isAnagram(String s, String t) {

Check warning on line 5 in Solved-Problems/Valid_Anagram/Main.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 'method def modifier' has incorrect indentation level 4, expected level should be 2. Raw Output: /github/workspace/./Solved-Problems/Valid_Anagram/Main.java:5:5: warning: 'method def modifier' has incorrect indentation level 4, expected level should be 2. (com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck)
if(s.length() != t.length())

Check warning on line 6 in Solved-Problems/Valid_Anagram/Main.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 'if' has incorrect indentation level 8, expected level should be 4. Raw Output: /github/workspace/./Solved-Problems/Valid_Anagram/Main.java:6:9: warning: 'if' has incorrect indentation level 8, expected level should be 4. (com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck)

Check warning on line 6 in Solved-Problems/Valid_Anagram/Main.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 'if' is not followed by whitespace. Raw Output: /github/workspace/./Solved-Problems/Valid_Anagram/Main.java:6:9: warning: 'if' is not followed by whitespace. (com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck)

Check warning on line 6 in Solved-Problems/Valid_Anagram/Main.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 WhitespaceAround: 'if' is not followed by whitespace. Empty blocks may only be represented as {} when not part of a multi-block statement (4.1.3) Raw Output: /github/workspace/./Solved-Problems/Valid_Anagram/Main.java:6:9: warning: WhitespaceAround: 'if' is not followed by whitespace. Empty blocks may only be represented as {} when not part of a multi-block statement (4.1.3) (com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAroundCheck)
{

Check warning on line 7 in Solved-Problems/Valid_Anagram/Main.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 'if lcurly' has incorrect indentation level 8, expected level should be 6. Raw Output: /github/workspace/./Solved-Problems/Valid_Anagram/Main.java:7:9: warning: 'if lcurly' has incorrect indentation level 8, expected level should be 6. (com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck)

Check warning on line 7 in Solved-Problems/Valid_Anagram/Main.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 '{' at column 9 should be on the previous line. Raw Output: /github/workspace/./Solved-Problems/Valid_Anagram/Main.java:7:9: warning: '{' at column 9 should be on the previous line. (com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck)
return false;

Check warning on line 8 in Solved-Problems/Valid_Anagram/Main.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 'if' child has incorrect indentation level 12, expected level should be one of the following: 6, 8. Raw Output: /github/workspace/./Solved-Problems/Valid_Anagram/Main.java:8:13: warning: 'if' child has incorrect indentation level 12, expected level should be one of the following: 6, 8. (com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck)
}

Check warning on line 9 in Solved-Problems/Valid_Anagram/Main.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 'if rcurly' has incorrect indentation level 8, expected level should be 6. Raw Output: /github/workspace/./Solved-Problems/Valid_Anagram/Main.java:9:9: warning: 'if rcurly' has incorrect indentation level 8, expected level should be 6. (com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck)

Map<Character, Integer> countS = new HashMap<>();

Check warning on line 11 in Solved-Problems/Valid_Anagram/Main.java

View workflow job for this annotation

GitHub Actions / java-linter / java-linter

[reviewdog] reported by reviewdog 🐶 'method def' child has incorrect indentation level 8, expected level should be 4. Raw Output: /github/workspace/./Solved-Problems/Valid_Anagram/Main.java:11:9: warning: 'method def' child has incorrect indentation level 8, expected level should be 4. (com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck)
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;
}
}
39 changes: 39 additions & 0 deletions Solved-Problems/Valid_Anagram/Readme.md
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.

0 comments on commit 9411d2e

Please sign in to comment.