Skip to content

Commit f50d174

Browse files
committed
feat: add solutions to lc problem: No.0242. Valid Anagram
1 parent 950c4f9 commit f50d174

File tree

6 files changed

+130
-4
lines changed

6 files changed

+130
-4
lines changed

solution/0100-0199/0122.Best Time to Buy and Sell Stock II/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -225,20 +225,20 @@ public class Solution {
225225

226226
动态规划:
227227

228-
```cs
229228
```cs
230229
public class Solution {
231230
public int MaxProfit(int[] prices) {
232231
int f1 = -prices[0], f2 = 0;
233-
for (int i = 1; i < prices.Length; ++i) {
232+
for (int i = 1; i < prices.Length; ++i)
233+
{
234234
f1 = Math.Max(f1, f2 - prices[i]);
235235
f2 = Math.Max(f2, f1 + prices[i]);
236236
}
237237
return f2;
238238
}
239239
}
240240
```
241-
```
241+
242242

243243
### **...**
244244

solution/0100-0199/0122.Best Time to Buy and Sell Stock II/README_EN.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ Dynamic Programming:
205205
public class Solution {
206206
public int MaxProfit(int[] prices) {
207207
int f1 = -prices[0], f2 = 0;
208-
for (int i = 1; i < prices.Length; ++i) {
208+
for (int i = 1; i < prices.Length; ++i)
209+
{
209210
f1 = Math.Max(f1, f2 - prices[i]);
210211
f2 = Math.Max(f2, f1 + prices[i]);
211212
}

solution/0200-0299/0242.Valid Anagram/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,51 @@ class Solution {
8080
}
8181
```
8282

83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
bool isAnagram(string s, string t) {
89+
if (s.size() != t.size()) {
90+
return false;
91+
}
92+
vector<int> chars(26, 0);
93+
for (int i = 0, n = s.size(); i < n; ++i) {
94+
++chars[s[i] - 'a'];
95+
--chars[t[i] - 'a'];
96+
}
97+
for (int i = 0; i < 26; ++i) {
98+
if (chars[i] != 0) {
99+
return false;
100+
}
101+
}
102+
return true;
103+
}
104+
};
105+
```
106+
107+
### **Go**
108+
109+
```go
110+
func isAnagram(s string, t string) bool {
111+
if len(s) != len(t) {
112+
return false
113+
}
114+
var chars [26]int
115+
for i := 0; i < len(s); i++ {
116+
chars[s[i]-'a']++
117+
chars[t[i]-'a']--
118+
}
119+
for i := 0; i < 26; i++ {
120+
if chars[i] != 0 {
121+
return false
122+
}
123+
}
124+
return true
125+
}
126+
```
127+
83128
### **...**
84129

85130
```

solution/0200-0299/0242.Valid Anagram/README_EN.md

+45
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,51 @@ class Solution {
7272
}
7373
```
7474

75+
### **C++**
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
bool isAnagram(string s, string t) {
81+
if (s.size() != t.size()) {
82+
return false;
83+
}
84+
vector<int> chars(26, 0);
85+
for (int i = 0, n = s.size(); i < n; ++i) {
86+
++chars[s[i] - 'a'];
87+
--chars[t[i] - 'a'];
88+
}
89+
for (int i = 0; i < 26; ++i) {
90+
if (chars[i] != 0) {
91+
return false;
92+
}
93+
}
94+
return true;
95+
}
96+
};
97+
```
98+
99+
### **Go**
100+
101+
```go
102+
func isAnagram(s string, t string) bool {
103+
if len(s) != len(t) {
104+
return false
105+
}
106+
var chars [26]int
107+
for i := 0; i < len(s); i++ {
108+
chars[s[i]-'a']++
109+
chars[t[i]-'a']--
110+
}
111+
for i := 0; i < 26; i++ {
112+
if chars[i] != 0 {
113+
return false
114+
}
115+
}
116+
return true
117+
}
118+
```
119+
75120
### **...**
76121

77122
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
bool isAnagram(string s, string t) {
4+
if (s.size() != t.size()) {
5+
return false;
6+
}
7+
vector<int> chars(26, 0);
8+
for (int i = 0, n = s.size(); i < n; ++i) {
9+
++chars[s[i] - 'a'];
10+
--chars[t[i] - 'a'];
11+
}
12+
for (int i = 0; i < 26; ++i) {
13+
if (chars[i] != 0) {
14+
return false;
15+
}
16+
}
17+
return true;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func isAnagram(s string, t string) bool {
2+
if len(s) != len(t) {
3+
return false
4+
}
5+
var chars [26]int
6+
for i := 0; i < len(s); i++ {
7+
chars[s[i]-'a']++
8+
chars[t[i]-'a']--
9+
}
10+
for i := 0; i < 26; i++ {
11+
if chars[i] != 0 {
12+
return false
13+
}
14+
}
15+
return true
16+
}

0 commit comments

Comments
 (0)