Skip to content

Commit 580aa0c

Browse files
authored
refactor: CheckVowels (#5393)
1 parent a5f57fb commit 580aa0c

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

src/main/java/com/thealgorithms/strings/CheckVowels.java

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.strings;
22

3-
import java.util.Arrays;
4-
import java.util.HashSet;
53
import java.util.Set;
64

75
/**
@@ -10,24 +8,24 @@
108
* alphabet. Wikipedia: https://en.wikipedia.org/wiki/Alphabetical_order
119
*/
1210
public final class CheckVowels {
11+
private static final Set<Character> VOWELS = Set.of('a', 'e', 'i', 'o', 'u');
12+
1313
private CheckVowels() {
1414
}
1515

16-
private static final Set<Character> VOWELS = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u'));
17-
1816
/**
19-
* Check if a string is has vowels or not
17+
* Checks if a string contains any vowels.
2018
*
21-
* @param input a string
22-
* @return {@code true} if given string has vowels, otherwise {@code false}
19+
* @param input a string to check
20+
* @return {@code true} if the given string contains at least one vowel, otherwise {@code false}
2321
*/
2422
public static boolean hasVowels(String input) {
25-
if (input == null) {
23+
if (input == null || input.isEmpty()) {
2624
return false;
2725
}
28-
input = input.toLowerCase();
29-
for (int i = 0; i < input.length(); i++) {
30-
if (VOWELS.contains(input.charAt(i))) {
26+
27+
for (char c : input.toLowerCase().toCharArray()) {
28+
if (VOWELS.contains(c)) {
3129
return true;
3230
}
3331
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package com.thealgorithms.strings;
22

3-
import static org.junit.jupiter.api.Assertions.assertFalse;
4-
import static org.junit.jupiter.api.Assertions.assertTrue;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
54

6-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
77

8-
public class CheckVowelsTest {
8+
class CheckVowelsTest {
99

10-
@Test
11-
public void isVowel() {
12-
assertTrue(CheckVowels.hasVowels("foo"));
13-
assertTrue(CheckVowels.hasVowels("bar"));
14-
assertFalse(CheckVowels.hasVowels("why"));
15-
assertFalse(CheckVowels.hasVowels("myths"));
10+
@ParameterizedTest
11+
@CsvSource({"'foo', true", "'bar', true", "'why', false", "'myths', false", "'', false", "'AEIOU', true", "'bcdfghjklmnpqrstvwxyz', false", "'AeIoU', true"})
12+
void testHasVowels(String input, boolean expected) {
13+
assertEquals(CheckVowels.hasVowels(input), expected);
1614
}
1715
}

0 commit comments

Comments
 (0)