Skip to content

Commit 629df4f

Browse files
committed
feat: add solutions to lc problem: No.1704
No.1704.Determine if String Halves Are Alike
1 parent 5a27bbb commit 629df4f

File tree

12 files changed

+182
-88
lines changed

12 files changed

+182
-88
lines changed

solution/1700-1799/1700.Number of Students Unable to Eat Lunch/README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@
6363

6464
因此,我们先用计数器 `cnt` 统计学生喜欢的三明治种类和对应的数量。
6565

66-
然后遍历三明治,若在 `cnt` 中找不到喜欢此三明治的学生,说明已经找到答案,当前以及往后的三明治均无法被拿走,数量为 $n - i$。
66+
然后遍历三明治,若在 `cnt` 中找不到喜欢此三明治的学生,说明后面的三明治也无法被拿走,返回当前剩余的学生数量。
67+
68+
遍历
6769

6870
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为三明治数量。
6971

@@ -77,9 +79,9 @@
7779
class Solution:
7880
def countStudents(self, students: List[int], sandwiches: List[int]) -> int:
7981
cnt = Counter(students)
80-
for i, v in enumerate(sandwiches):
82+
for v in sandwiches:
8183
if cnt[v] == 0:
82-
return len(students) - i
84+
return cnt[v ^ 1]
8385
cnt[v] -= 1
8486
return 0
8587
```
@@ -95,10 +97,9 @@ class Solution {
9597
for (int v : students) {
9698
++cnt[v];
9799
}
98-
int n = students.length;
99-
for (int i = 0; i < n; ++i) {
100-
if (cnt[sandwiches[i]]-- == 0) {
101-
return n - i;
100+
for (int v : sandwiches) {
101+
if (cnt[v]-- == 0) {
102+
return cnt[v ^ 1];
102103
}
103104
}
104105
return 0;
@@ -114,10 +115,9 @@ public:
114115
int countStudents(vector<int>& students, vector<int>& sandwiches) {
115116
int cnt[2] = {0};
116117
for (int& v : students) ++cnt[v];
117-
int n = students.size();
118-
for (int i = 0; i < n; ++i) {
119-
if (cnt[sandwiches[i]]-- == 0) {
120-
return n - i;
118+
for (int& v : sandwiches) {
119+
if (cnt[v]-- == 0) {
120+
return cnt[v ^ 1];
121121
}
122122
}
123123
return 0;
@@ -133,9 +133,9 @@ func countStudents(students []int, sandwiches []int) int {
133133
for _, v := range students {
134134
cnt[v]++
135135
}
136-
for i, v := range sandwiches {
136+
for _, v := range sandwiches {
137137
if cnt[v] == 0 {
138-
return len(students) - i
138+
return cnt[v^1]
139139
}
140140
cnt[v]--
141141
}

solution/1700-1799/1700.Number of Students Unable to Eat Lunch/README_EN.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ Hence all students are able to eat.
6262
class Solution:
6363
def countStudents(self, students: List[int], sandwiches: List[int]) -> int:
6464
cnt = Counter(students)
65-
for i, v in enumerate(sandwiches):
65+
for v in sandwiches:
6666
if cnt[v] == 0:
67-
return len(students) - i
67+
return cnt[v ^ 1]
6868
cnt[v] -= 1
6969
return 0
7070
```
@@ -78,10 +78,9 @@ class Solution {
7878
for (int v : students) {
7979
++cnt[v];
8080
}
81-
int n = students.length;
82-
for (int i = 0; i < n; ++i) {
83-
if (cnt[sandwiches[i]]-- == 0) {
84-
return n - i;
81+
for (int v : sandwiches) {
82+
if (cnt[v]-- == 0) {
83+
return cnt[v ^ 1];
8584
}
8685
}
8786
return 0;
@@ -97,10 +96,9 @@ public:
9796
int countStudents(vector<int>& students, vector<int>& sandwiches) {
9897
int cnt[2] = {0};
9998
for (int& v : students) ++cnt[v];
100-
int n = students.size();
101-
for (int i = 0; i < n; ++i) {
102-
if (cnt[sandwiches[i]]-- == 0) {
103-
return n - i;
99+
for (int& v : sandwiches) {
100+
if (cnt[v]-- == 0) {
101+
return cnt[v ^ 1];
104102
}
105103
}
106104
return 0;
@@ -116,9 +114,9 @@ func countStudents(students []int, sandwiches []int) int {
116114
for _, v := range students {
117115
cnt[v]++
118116
}
119-
for i, v := range sandwiches {
117+
for _, v := range sandwiches {
120118
if cnt[v] == 0 {
121-
return len(students) - i
119+
return cnt[v^1]
122120
}
123121
cnt[v]--
124122
}

solution/1700-1799/1700.Number of Students Unable to Eat Lunch/Solution.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ class Solution {
33
int countStudents(vector<int>& students, vector<int>& sandwiches) {
44
int cnt[2] = {0};
55
for (int& v : students) ++cnt[v];
6-
int n = students.size();
7-
for (int i = 0; i < n; ++i) {
8-
if (cnt[sandwiches[i]]-- == 0) {
9-
return n - i;
6+
for (int& v : sandwiches) {
7+
if (cnt[v]-- == 0) {
8+
return cnt[v ^ 1];
109
}
1110
}
1211
return 0;

solution/1700-1799/1700.Number of Students Unable to Eat Lunch/Solution.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ func countStudents(students []int, sandwiches []int) int {
33
for _, v := range students {
44
cnt[v]++
55
}
6-
for i, v := range sandwiches {
6+
for _, v := range sandwiches {
77
if cnt[v] == 0 {
8-
return len(students) - i
8+
return cnt[v^1]
99
}
1010
cnt[v]--
1111
}

solution/1700-1799/1700.Number of Students Unable to Eat Lunch/Solution.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ public int countStudents(int[] students, int[] sandwiches) {
44
for (int v : students) {
55
++cnt[v];
66
}
7-
int n = students.length;
8-
for (int i = 0; i < n; ++i) {
9-
if (cnt[sandwiches[i]]-- == 0) {
10-
return n - i;
7+
for (int v : sandwiches) {
8+
if (cnt[v]-- == 0) {
9+
return cnt[v ^ 1];
1110
}
1211
}
1312
return 0;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution:
22
def countStudents(self, students: List[int], sandwiches: List[int]) -> int:
33
cnt = Counter(students)
4-
for i, v in enumerate(sandwiches):
4+
for v in sandwiches:
55
if cnt[v] == 0:
6-
return len(students) - i
6+
return cnt[v ^ 1]
77
cnt[v] -= 1
88
return 0

solution/1700-1799/1704.Determine if String Halves Are Alike/README.md

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:计数**
49+
50+
遍历字符串,若字符串前半段的元音个数等于后半段的元音个数,则返回 `true`,否则返回 `false`
51+
52+
时间复杂度 $O(n)$,其中 $n$ 为字符串的长度。
53+
4854
<!-- tabs:start -->
4955

5056
### **Python3**
@@ -54,11 +60,12 @@
5460
```python
5561
class Solution:
5662
def halvesAreAlike(self, s: str) -> bool:
57-
half = len(s) >> 1
58-
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
59-
s1 = sum(1 for c in s[:half] if c in vowels)
60-
s2 = sum(1 for c in s[half:] if c in vowels)
61-
return s1 == s2
63+
cnt, n = 0, len(s) >> 1
64+
vowels = set('aeiouAEIOU')
65+
for i in range(n):
66+
cnt += s[i] in vowels
67+
cnt -= s[i + n] in vowels
68+
return cnt == 0
6269
```
6370

6471
### **Java**
@@ -67,24 +74,57 @@ class Solution:
6774

6875
```java
6976
class Solution {
77+
private static final Set<Character> VOWELS = Set.of('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');
78+
7079
public boolean halvesAreAlike(String s) {
71-
int half = s.length() >> 1;
72-
Set<Character> vowels
73-
= new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
74-
int s1 = 0, s2 = 0;
75-
for (int i = 0; i < half; ++i) {
76-
if (vowels.contains(s.charAt(i))) {
77-
++s1;
78-
}
79-
if (vowels.contains(s.charAt(half + i))) {
80-
++s2;
81-
}
80+
int cnt = 0, n = s.length() >> 1;
81+
for (int i = 0; i < n; ++i) {
82+
cnt += VOWELS.contains(s.charAt(i)) ? 1 : 0;
83+
cnt -= VOWELS.contains(s.charAt(i + n)) ? 1 : 0;
8284
}
83-
return s1 == s2;
85+
return cnt == 0;
8486
}
8587
}
8688
```
8789

90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
bool halvesAreAlike(string s) {
96+
unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
97+
int cnt = 0, n = s.size() / 2;
98+
for (int i = 0; i < n; ++i) {
99+
cnt += vowels.count(s[i]);
100+
cnt -= vowels.count(s[i + n]);
101+
}
102+
return cnt == 0;
103+
}
104+
};
105+
```
106+
107+
### **Go**
108+
109+
```go
110+
func halvesAreAlike(s string) bool {
111+
vowels := map[byte]bool{}
112+
for _, c := range "aeiouAEIOU" {
113+
vowels[byte(c)] = true
114+
}
115+
cnt, n := 0, len(s)>>1
116+
for i := 0; i < n; i++ {
117+
if vowels[s[i]] {
118+
cnt++
119+
}
120+
if vowels[s[i+n]] {
121+
cnt--
122+
}
123+
}
124+
return cnt == 0
125+
}
126+
```
127+
88128
### **...**
89129

90130
```

solution/1700-1799/1704.Determine if String Halves Are Alike/README_EN.md

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,32 +46,66 @@ Notice that the vowel o is counted twice.
4646
```python
4747
class Solution:
4848
def halvesAreAlike(self, s: str) -> bool:
49-
half = len(s) >> 1
50-
vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
51-
s1 = sum(1 for c in s[:half] if c in vowels)
52-
s2 = sum(1 for c in s[half:] if c in vowels)
53-
return s1 == s2
49+
cnt, n = 0, len(s) >> 1
50+
vowels = set('aeiouAEIOU')
51+
for i in range(n):
52+
cnt += s[i] in vowels
53+
cnt -= s[i + n] in vowels
54+
return cnt == 0
5455
```
5556

5657
### **Java**
5758

5859
```java
5960
class Solution {
61+
private static final Set<Character> VOWELS = Set.of('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');
62+
6063
public boolean halvesAreAlike(String s) {
61-
int half = s.length() >> 1;
62-
Set<Character> vowels
63-
= new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
64-
int s1 = 0, s2 = 0;
65-
for (int i = 0; i < half; ++i) {
66-
if (vowels.contains(s.charAt(i))) {
67-
++s1;
68-
}
69-
if (vowels.contains(s.charAt(half + i))) {
70-
++s2;
71-
}
64+
int cnt = 0, n = s.length() >> 1;
65+
for (int i = 0; i < n; ++i) {
66+
cnt += VOWELS.contains(s.charAt(i)) ? 1 : 0;
67+
cnt -= VOWELS.contains(s.charAt(i + n)) ? 1 : 0;
68+
}
69+
return cnt == 0;
70+
}
71+
}
72+
```
73+
74+
### **C++**
75+
76+
```cpp
77+
class Solution {
78+
public:
79+
bool halvesAreAlike(string s) {
80+
unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
81+
int cnt = 0, n = s.size() / 2;
82+
for (int i = 0; i < n; ++i) {
83+
cnt += vowels.count(s[i]);
84+
cnt -= vowels.count(s[i + n]);
7285
}
73-
return s1 == s2;
86+
return cnt == 0;
7487
}
88+
};
89+
```
90+
91+
### **Go**
92+
93+
```go
94+
func halvesAreAlike(s string) bool {
95+
vowels := map[byte]bool{}
96+
for _, c := range "aeiouAEIOU" {
97+
vowels[byte(c)] = true
98+
}
99+
cnt, n := 0, len(s)>>1
100+
for i := 0; i < n; i++ {
101+
if vowels[s[i]] {
102+
cnt++
103+
}
104+
if vowels[s[i+n]] {
105+
cnt--
106+
}
107+
}
108+
return cnt == 0
75109
}
76110
```
77111

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
bool halvesAreAlike(string s) {
4+
unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
5+
int cnt = 0, n = s.size() / 2;
6+
for (int i = 0; i < n; ++i) {
7+
cnt += vowels.count(s[i]);
8+
cnt -= vowels.count(s[i + n]);
9+
}
10+
return cnt == 0;
11+
}
12+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func halvesAreAlike(s string) bool {
2+
vowels := map[byte]bool{}
3+
for _, c := range "aeiouAEIOU" {
4+
vowels[byte(c)] = true
5+
}
6+
cnt, n := 0, len(s)>>1
7+
for i := 0; i < n; i++ {
8+
if vowels[s[i]] {
9+
cnt++
10+
}
11+
if vowels[s[i+n]] {
12+
cnt--
13+
}
14+
}
15+
return cnt == 0
16+
}

0 commit comments

Comments
 (0)