Skip to content

Commit f60debd

Browse files
Merge pull request eugenp#5046 from kartiksingla/BAEL-2141
[BAEL-2141] - How to check if a string contains all the letters of the alphabet?
2 parents 9b343be + 1846a1e commit f60debd

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.algorithms.string;
2+
3+
public class EnglishAlphabetLetters {
4+
5+
public static boolean checkStringForAllTheLetters(String input) {
6+
boolean[] visited = new boolean[26];
7+
8+
int index = 0;
9+
10+
for (int id = 0; id < input.length(); id++) {
11+
if ('a' <= input.charAt(id) && input.charAt(id) <= 'z') {
12+
index = input.charAt(id) - 'a';
13+
} else if ('A' <= input.charAt(id) && input.charAt(id) <= 'Z') {
14+
index = input.charAt(id) - 'A';
15+
}
16+
visited[index] = true;
17+
}
18+
19+
for (int id = 0; id < 26; id++) {
20+
if (!visited[id]) {
21+
return false;
22+
}
23+
}
24+
return true;
25+
}
26+
27+
public static boolean checkStringForAllLetterUsingStream(String input) {
28+
long c = input.toLowerCase().chars().filter(ch -> ch >= 'a' && ch <= 'z').distinct().count();
29+
return c == 26;
30+
}
31+
32+
public static void main(String[] args) {
33+
checkStringForAllLetterUsingStream("intit");
34+
}
35+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.algorithms.string;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class EnglishAlphabetLettersUnitTest {
7+
8+
@Test
9+
void givenString_whenContainsAllCharacter_thenTrue() {
10+
String input = "Farmer jack realized that big yellow quilts were expensive";
11+
Assertions.assertTrue(EnglishAlphabetLetters.checkStringForAllTheLetters(input));
12+
}
13+
14+
@Test
15+
void givenString_whenContainsAllCharacter_thenUsingStreamExpectTrue() {
16+
String input = "Farmer jack realized that big yellow quilts were expensive";
17+
Assertions.assertTrue(EnglishAlphabetLetters.checkStringForAllLetterUsingStream(input));
18+
}
19+
20+
}

0 commit comments

Comments
 (0)