9
9
10
10
<p ><strong >示例 1:</strong ></p >
11
11
12
- <pre ><strong >输入:</strong > < code >s1</ code > = " ; abc" ; , < code >s2</ code > = " ; bca" ;
12
+ <pre ><strong >输入:</strong > s1 = " ; abc" ; , s2 = " ; bca" ;
13
13
<strong >输出:</strong > true
14
14
</pre >
15
15
16
16
<p ><strong >示例 2:</strong ></p >
17
17
18
- <pre ><strong >输入:</strong > < code >s1</ code > = " ; abc" ; , < code >s2</ code > = " ; bad" ;
18
+ <pre ><strong >输入:</strong > s1 = " ; abc" ; , s2 = " ; bad" ;
19
19
<strong >输出:</strong > false
20
20
</pre >
21
21
@@ -48,10 +48,7 @@ class Solution:
48
48
for i in range (n1):
49
49
counter[s1[i]] += 1
50
50
counter[s2[i]] -= 1
51
- for val in counter.values():
52
- if val != 0 :
53
- return False
54
- return True
51
+ return all (v == 0 for v in counter.values())
55
52
```
56
53
57
54
### ** Java**
@@ -61,18 +58,18 @@ class Solution:
61
58
``` java
62
59
class Solution {
63
60
public boolean CheckPermutation (String s1 , String s2 ) {
64
- int n1 = s1. length(), n2 = s2. length();
61
+ int n1 = s1. length();
62
+ int n2 = s2. length();
65
63
if (n1 != n2) {
66
64
return false ;
67
65
}
68
- Map< Character , Integer > counter = new HashMap<> () ;
66
+ int [] counter = new int [ 128 ] ;
69
67
for (int i = 0 ; i < n1; ++ i) {
70
- char c1 = s1. charAt(i), c2 = s2. charAt(i);
71
- counter. put(c1, counter. getOrDefault(c1, 0 ) + 1 );
72
- counter. put(c2, counter. getOrDefault(c2, 0 ) - 1 );
68
+ ++ counter[s1. charAt(i)];
69
+ -- counter[s2. charAt(i)];
73
70
}
74
- for (int val : counter. values() ) {
75
- if (val != 0 ) {
71
+ for (int v : counter) {
72
+ if (v != 0 ) {
76
73
return false ;
77
74
}
78
75
}
0 commit comments