mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #1611 from RyouMon/master
优化 0242.有效的字母异位词.md Java解法
This commit is contained in:
@ -92,18 +92,24 @@ public:
|
||||
|
||||
Java:
|
||||
```java
|
||||
/**
|
||||
* 242. 有效的字母异位词 字典解法
|
||||
* 时间复杂度O(m+n) 空间复杂度O(1)
|
||||
*/
|
||||
class Solution {
|
||||
public boolean isAnagram(String s, String t) {
|
||||
|
||||
int[] record = new int[26];
|
||||
for (char c : s.toCharArray()) {
|
||||
record[c - 'a'] += 1;
|
||||
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
record[s.charAt(i) - 'a']++;
|
||||
}
|
||||
for (char c : t.toCharArray()) {
|
||||
record[c - 'a'] -= 1;
|
||||
|
||||
for (int i = 0; i < t.length(); i++) {
|
||||
record[t.charAt(i) - 'a']--;
|
||||
}
|
||||
for (int i : record) {
|
||||
if (i != 0) {
|
||||
|
||||
for (int count: record) {
|
||||
if (count != 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user