Skip to content

Commit 808a77b

Browse files
committed
leetcode 242
1 parent 135787b commit 808a77b

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

201-250/242.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## :pencil2:Leetcode之PHP版题目解析(242. Valid Anagram)
2+
**2020-02-26 吴亲库里 库里的深夜食堂**
3+
****
4+
### :pencil2:描述
5+
**给定两个字符串 s 和 t,判断 t 是否是 s 的有效的字母异位词 **
6+
****
7+
### :pencil2:题目实例
8+
<a href="https://github.com/wuqinqiang/">
9+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/images/242.png">
10+
</a>
11+
****
12+
13+
### :pencil2:题目分析
14+
**有限的字母异位词是什么意思呢。说白了就是字符串的字母组成都是一样的,只是字母出现的位置不一样。那么首先他们的长度肯定是相同的,不同的就一定不是。原理其实就是可以使用 map 记录 s 字符串每个单词出现的频率,用 t 减少相同字符的频率,最后只要判断计数器中是否还存值即可。**
15+
16+
```php
17+
18+
/**
19+
* @param String $s
20+
* @param String $t
21+
* @return Boolean
22+
*/
23+
function isAnagram($s, $t)
24+
{
25+
if (strlen($s) != strlen($t)) {
26+
return false;
27+
}
28+
$data = [];
29+
for ($i = 0; $i < strlen($s); $i++) {
30+
if (!$data[$s[$i]]) $data[$s[$i]] = 0;
31+
if (!$data[$t[$i]]) $data[$t[$i]] = 0;
32+
$data[$s[$i]]++;
33+
$data[$t[$i]]--;
34+
}
35+
foreach ($data as $value) {
36+
if ($value) return false;
37+
}
38+
return true;
39+
}
40+
```
41+
42+
43+
### 联系
44+
45+
<a href="https://github.com/wuqinqiang/">
46+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/qrcode_for_gh_c194f9d4cdb1_430.jpg" width="150px" height="150px">
47+
</a>
48+
49+
50+
51+
52+

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
- [Leetcode238](https://github.com/wuqinqiang/Lettcode-php/blob/master/201-250/238.md)
191191
- [Leetcode234](https://github.com/wuqinqiang/Lettcode-php/blob/master/201-250/234.md)
192192
- [Leetcode240](https://github.com/wuqinqiang/Lettcode-php/blob/master/201-250/240.md)
193+
- [Leetcode242](https://github.com/wuqinqiang/Lettcode-php/blob/master/201-250/242.md)
193194
****
194195

195196
**251-300**

images/242.png

119 KB
Loading

0 commit comments

Comments
 (0)