Skip to content

Commit 4451eb6

Browse files
committed
test: 3541 solution
py, c++, go, java
1 parent b7137f1 commit 4451eb6

File tree

3 files changed

+65
-15
lines changed

3 files changed

+65
-15
lines changed
Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
//go:build ignore
22
#include "cpp/common/Solution.h"
3-
3+
#include <algorithm>
44

55
using namespace std;
66
using json = nlohmann::json;
77

8+
constexpr string VOWELS = "aeiou";
9+
810
class Solution {
911
public:
10-
int maxFreqSum(string s) {
11-
12+
int maxFreqSum(const string &s) {
13+
array<int, 5> vowels_count = {};
14+
array<int, 26> non_vowels_count = {};
15+
for (const auto &c : s) {
16+
size_t pos = VOWELS.find(c);
17+
if (pos != std::string::npos) {
18+
++vowels_count[pos];
19+
} else {
20+
++non_vowels_count[c - 'a'];
21+
}
1222
}
23+
return *std::max_element(vowels_count.begin(), vowels_count.end()) +
24+
*std::max_element(non_vowels_count.begin(), non_vowels_count.end());
25+
}
1326
};
1427

1528
json leetcode::qubh::Solve(string input_json_values) {
16-
vector<string> inputArray;
17-
size_t pos = input_json_values.find('\n');
18-
while (pos != string::npos) {
19-
inputArray.push_back(input_json_values.substr(0, pos));
20-
input_json_values = input_json_values.substr(pos + 1);
21-
pos = input_json_values.find('\n');
22-
}
23-
inputArray.push_back(input_json_values);
29+
vector<string> inputArray;
30+
size_t pos = input_json_values.find('\n');
31+
while (pos != string::npos) {
32+
inputArray.push_back(input_json_values.substr(0, pos));
33+
input_json_values = input_json_values.substr(pos + 1);
34+
pos = input_json_values.find('\n');
35+
}
36+
inputArray.push_back(input_json_values);
2437

25-
Solution solution;
26-
string s = json::parse(inputArray.at(0));
27-
return solution.maxFreqSum(s);
38+
Solution solution;
39+
string s = json::parse(inputArray.at(0));
40+
return solution.maxFreqSum(s);
2841
}

problems/problems_3541/Solution.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,27 @@
66

77

88
public class Solution extends BaseSolution {
9+
private static final String VOWELS = "aeiou";
10+
911
public int maxFreqSum(String s) {
10-
12+
int[] vowelsCount = new int[5], nonVowesCount = new int[26];
13+
for (char c: s.toCharArray()) {
14+
int idx = VOWELS.indexOf(c);
15+
if (idx != -1) {
16+
++vowelsCount[idx];
17+
} else {
18+
++nonVowesCount[c - 'a'];
19+
}
20+
}
21+
return arrayMax(vowelsCount) + arrayMax(nonVowesCount);
22+
}
23+
24+
private int arrayMax(int[] arr) {
25+
int ans = 0;
26+
for (int v: arr) {
27+
ans = Math.max(ans, v);
28+
}
29+
return ans;
1130
}
1231

1332
@Override

problems/problems_3541/solution.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,26 @@ import (
66
"strings"
77
)
88

9+
const VOWELS = "aeiou"
10+
911
func maxFreqSum(s string) int {
12+
vowelsCount := make([]int, 5)
13+
nonVowelsCount := make([]int, 26)
14+
for _, r := range s {
15+
if idx := strings.IndexRune(VOWELS, r); idx >= 0 {
16+
vowelsCount[idx]++
17+
} else {
18+
nonVowelsCount[r-'a']++
19+
}
20+
}
21+
return maxElement(vowelsCount) + maxElement(nonVowelsCount)
22+
}
1023

24+
func maxElement(arr []int) (ans int) {
25+
for _, v := range arr {
26+
ans = max(ans, v)
27+
}
28+
return
1129
}
1230

1331
func Solve(inputJsonValues string) any {

0 commit comments

Comments
 (0)