|
34 | 34 |
|
35 | 35 | ## Solutions
|
36 | 36 |
|
| 37 | +**Solution 1: Array or Hash Table** |
| 38 | + |
| 39 | +First, we check whether the lengths of the two strings are equal. If they are not equal, we directly return `false`. |
| 40 | + |
| 41 | +Then, we use an array or hash table to count the occurrence of each character in string $s1$. |
| 42 | + |
| 43 | +Next, we traverse the other string $s2$. For each character we encounter, we decrement its corresponding count. If the count after decrementing is less than 0ドル,ドル it means that the occurrence of characters in the two strings is different, so we directly return `false`. |
| 44 | + |
| 45 | +Finally, after traversing string $s2,ドル we return `true`. |
| 46 | + |
| 47 | +Note: In this problem, all test case strings only contain lowercase letters, so we can directly create an array of length 26ドル$ for counting. |
| 48 | + |
| 49 | +The time complexity is $O(n),ドル and the space complexity is $O(C)$. Here, $n$ is the length of the string, and $C$ is the size of the character set. In this problem, $C=26$. |
| 50 | + |
| 51 | +**Solution 2: Sorting** |
| 52 | + |
| 53 | +We can also sort the two strings in lexicographical order, and then compare whether the two strings are equal. |
| 54 | + |
| 55 | +The time complexity is $O(n \times \log n),ドル and the space complexity is $O(n)$. Here, $n$ is the length of the string. |
| 56 | + |
37 | 57 | <!-- tabs:start -->
|
38 | 58 |
|
39 | 59 | ### **Python3**
|
|
0 commit comments