Skip to content

Commit 04beec1

Browse files
committed
https://leetcode.com/problems/valid-anagram/
1 parent b1fed93 commit 04beec1

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+

2+
namespace LeetCodeSolutions.HashMap
3+
{
4+
class Valid_Anagram
5+
{
6+
public bool IsAnagram(string s, string t)
7+
{
8+
if (s.Length != t.Length) return false;
9+
10+
Dictionary<char, int> counts = new Dictionary<char, int>();
11+
12+
foreach (char c in s)
13+
{
14+
counts[c] = counts.GetValueOrDefault(c, 0) + 1;
15+
}
16+
17+
foreach (char c in t)
18+
{
19+
if (!counts.ContainsKey(c) || counts[c] == 0) return false;
20+
counts[c]--;
21+
}
22+
23+
return true;
24+
}
25+
26+
[Test(Description = "https://leetcode.com/problems/valid-anagram/")]
27+
[Category("Easy")]
28+
[Category("LeetCode")]
29+
[Category("Valid Anagram")]
30+
[TestCaseSource(nameof(Input))]
31+
[Category("HashMap")]
32+
[Category("TopInterview")]
33+
public void Test1((bool Output, (string, string) Input) item)
34+
{
35+
var response = IsAnagram(item.Input.Item1, item.Input.Item2);
36+
Assert.That(response, Is.EqualTo(item.Output));
37+
}
38+
39+
public static IEnumerable<(bool Output, (string, string) Input)> Input =>
40+
new List<(bool Output, (string, string) Input)>()
41+
{
42+
43+
(true, ("anagram", "nagaram")),
44+
(false, ("rat", "car")),
45+
};
46+
}
47+
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![](https://img.shields.io/badge/Neovim-57A143.svg?logo=Neovim&amp;logoColor=white)
44
![](https://img.shields.io/badge/Visual_Studio_Code-0078D4?logo=visual%20studio%20code&amp;logoColor=white)
5-
![](https://img.shields.io/badge/Progress-26%2F150-0078D4)
5+
![](https://img.shields.io/badge/Progress-27%2F150-0078D4)
66

77
This repository contains solutions to the Leetcode Top Interview 150 problems.
88

@@ -89,7 +89,7 @@ The Top Interview 150 collection on Leetcode is a curated set of 150 interview q
8989
| 39 | Ransom Note | Easy ||
9090
| 40 | Isomorphic Strings | Easy ||
9191
| 41 | Word Pattern | Easy | |
92-
| 42 | Valid Anagram | Easy | |
92+
| 42 | Valid Anagram | Easy | |
9393
| 43 | Group Anagrams | Medium | |
9494
| 44 | Two Sum | Easy | |
9595
| 45 | Happy Number | Easy | |

0 commit comments

Comments
 (0)