mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
优化 242. 有效的字母异位词 Java解法
This commit is contained in:
@ -92,18 +92,24 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
```java
|
```java
|
||||||
|
/**
|
||||||
|
* 242. 有效的字母异位词 字典解法
|
||||||
|
* 时间复杂度O(m+n) 空间复杂度O(1)
|
||||||
|
*/
|
||||||
class Solution {
|
class Solution {
|
||||||
public boolean isAnagram(String s, String t) {
|
public boolean isAnagram(String s, String t) {
|
||||||
|
|
||||||
int[] record = new int[26];
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user