Skip to content

Commit b5dc12b

Browse files
author
Li Li
committed
rename all the solution and add 242
1 parent bf870c2 commit b5dc12b

File tree

14 files changed

+55
-16
lines changed

14 files changed

+55
-16
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Solution {
2+
public bool IsAnagram(string s, string t) {
3+
if (s == null) return t == null;
4+
if (t == null) return false;
5+
int m = s.Length, n = t.Length;
6+
if (m != n) return false;
7+
var dict = new Dictionary<char, int>();
8+
for (var i = 0; i < m; i++) {
9+
if (!dict.ContainsKey(s[i])) {
10+
dict[s[i]] = 1;
11+
} else {
12+
dict[s[i]]++;
13+
}
14+
}
15+
for (var i = 0; i < n; i++) {
16+
if (!dict.ContainsKey(t[i])) {
17+
return false;
18+
}
19+
dict[t[i]]--;
20+
if (dict[t[i]] < 0) {
21+
return false;
22+
}
23+
}
24+
return true;
25+
}
26+
}
File renamed without changes.
File renamed without changes.

LeetcodeCShaprDotNetCore/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace LeetcodeCShaprDotNetCore {
44
class Program {
55
static void Main(string[] args) {
66
var s = new Solution();
7-
var r = s.TwoSum(new int[] { 2, 7, 11, 15 }, 9);
7+
var r = s.TwoSum(new int[] { 11, 7, 2, 15 }, 9);
88
System.Console.WriteLine(r[0] + " " + r[1]);
99
}
1010
}

LeetcodeCShaprDotNetCore/Solution.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@
44

55
namespace LeetcodeCShaprDotNetCore {
66
public class Solution {
7-
public int[] TwoSum(int[] nums, int target) {
8-
int[] res = null;
9-
if (nums == null || nums.Length == 0) {
10-
return res;
7+
public bool IsAnagram(string s, string t) {
8+
if (s == null) return t == null;
9+
if (t == null) return false;
10+
int m = s.Length, n = t.Length;
11+
if (m != n) return false;
12+
var dict = new Dictionary<char, int>();
13+
for (var i = 0; i < m; i++) {
14+
if (!dict.ContainsKey(s[i])) {
15+
dict[s[i]] = 1;
16+
} else {
17+
dict[s[i]]++;
18+
}
1119
}
12-
for (int i = 0, j = nums.Length - 1; i < j;) {
13-
var v = nums[i] + nums[j];
14-
if (v == target) {
15-
res = new int[] { i + 1, j + 1 };
16-
break;
20+
for (var i = 0; i < n; i++) {
21+
if (!dict.ContainsKey(t[i])) {
22+
return false;
1723
}
18-
if (v < target) {
19-
i++;
20-
} else {
21-
j--;
24+
dict[t[i]]--;
25+
if (dict[t[i]] < 0) {
26+
return false;
2227
}
2328
}
24-
return res;
29+
return true;
2530
}
2631
}
2732
}

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,12 @@ Since the array is already sorted, we can keep two pointers *left* and *right*.
5555

5656
Similar problems:
5757
*1.* Two Sum (use dict for each item just use almost O(1) check time, so totally O(n) time);
58-
*653.* Two Sum IV - Input is a BST (3 solution to revisit)
58+
*653.* Two Sum IV - Input is a BST (3 solution to revisit)
59+
60+
#### 242. Valid Anagram
61+
62+
This problem can be solved by using Dictionary.
63+
64+
**Algorithm**
65+
First use dict record the counts for each char in string1, then check for string2, for each char c, if !dict.ContainsKey(c), return fasle; else dict[c]--, if dict[c] < 0, return false.
66+

0 commit comments

Comments
 (0)