Skip to content

Commit f4de1c9

Browse files
committed
Include leetcode problem.
1 parent 6fbd5c0 commit f4de1c9

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/test/java/com/github/streams/practice/b_medium/strings/StringProblemsSolution.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,28 @@ public static List<String> findDuplicateStrings(List<String> input) {
8080
public static String convertListOfCharactersToString(Collection<Character> listOfCharacters) {
8181
return listOfCharacters.stream().map(Object::toString).reduce("", String::concat);
8282
}
83+
public static int frequentVowelPlusConstant(String s){
84+
final var vowels = Set.of('a', 'e', 'i', 'o', 'u');
85+
final var vowelsValue = s.chars()
86+
.mapToObj(e -> Character.valueOf((char) e))
87+
.filter(vowels::contains)
88+
.collect(Collectors.toMap(Function.identity(), c -> 1, Integer::sum))
89+
.entrySet()
90+
.stream()
91+
.max(Map.Entry.comparingByValue())
92+
.map(Map.Entry::getValue)
93+
.orElse(0);
94+
95+
final var constValue = s.chars()
96+
.mapToObj(e -> Character.valueOf((char) e))
97+
.filter(e->!vowels.contains(e))
98+
.collect(Collectors.toMap(Function.identity(), c -> 1, Integer::sum))
99+
.entrySet()
100+
.stream()
101+
.max(Map.Entry.comparingByValue())
102+
.map(Map.Entry::getValue)
103+
.orElse(0);
104+
105+
return vowelsValue + constValue;
106+
}
83107
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.github.streams.practice.b_medium.strings.problems;
2+
3+
import com.github.streams.practice.b_medium.strings.StringProblemsSolution;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Disabled;
6+
import org.junit.jupiter.api.Test;
7+
8+
///
9+
/// You are given a string s consisting of lowercase English letters ('a' to 'z').
10+
///
11+
/// Your task is to:
12+
///
13+
/// Find the vowel (one of 'a', 'e', 'i', 'o', or 'u') with the maximum frequency.
14+
/// Find the consonant (all other letters excluding vowels) with the maximum frequency.
15+
/// Return the sum of the two frequencies.
16+
///
17+
/// Note: If multiple vowels or consonants have the same maximum frequency, you may choose any one of them. If there are no vowels or no consonants in the string, consider their frequency as 0.
18+
///
19+
///
20+
///
21+
/// The frequency of a letter x is the number of times it occurs in the string.
22+
///
23+
///
24+
/// Example 1:
25+
///
26+
/// Input: s = "successes"
27+
///
28+
/// Output: 6
29+
///
30+
/// Explanation:
31+
///
32+
/// The vowels are: 'u' (frequency 1), 'e' (frequency 2). The maximum frequency is 2.
33+
/// The consonants are: 's' (frequency 4), 'c' (frequency 2). The maximum frequency is 4.
34+
/// The output is 2 + 4 = 6.
35+
/// Example 2:
36+
///
37+
/// Input: s = "aeiaeia"
38+
///
39+
/// Output: 3
40+
///
41+
/// Explanation:
42+
///
43+
/// The vowels are: 'a' (frequency 3), 'e' ( frequency 2), 'i' (frequency 2). The maximum frequency is 3.
44+
/// There are no consonants in s. Hence, maximum consonant frequency = 0.
45+
/// The output is 3 + 0 = 3.
46+
///
47+
///
48+
/// Constraints:
49+
///
50+
/// 1 <= s.length <= 100
51+
/// s consists of lowercase English letters only.
52+
///
53+
///
54+
///
55+
///
56+
///
57+
///
58+
///
59+
class Q_FrequentVowelPlusConstant {
60+
61+
@Test
62+
@Disabled
63+
void freqVowelsPlusConstant() {
64+
final var input = "successes";
65+
final var mySolution = StringProblemsSolution.frequentVowelPlusConstant(input);
66+
final var yourSolution = -1;
67+
68+
Assertions.assertEquals(mySolution, yourSolution);
69+
}
70+
}

0 commit comments

Comments
 (0)