-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCheck if the Sentence Is Pangram.java
61 lines (47 loc) · 1.21 KB
/
Check if the Sentence Is Pangram.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class Solution {
/**
* Time: O(n)
* Memory: O(1)
*/
public boolean checkIfPangram(String sentence) {
int seen = 0;
for (char ch : sentence.toCharArray())
seen |= 1 << (ch - 'a');
return seen == (1 << 26) - 1;
}
}
public class Solution {
/**
* Time: O(n)
* Memory: O(1)
*/
public boolean checkIfPangram(String sentence) {
int[] powers = new int[27];
powers[0] = 1;
for (int i = 1; i < powers.length; ++i)
powers[i] = powers[i - 1] << 1;
int seen = 0;
for (char ch : sentence.toCharArray())
seen |= powers[ch - 'a'];
return seen == powers[26] - 1;
}
}
public class Solution {
/**
* Time: O(n)
* Memory: O(1)
*/
public boolean checkIfPangram(String sentence) {
return sentence.chars().reduce(0, (seen, ch) -> seen | 1 << (ch - 'a')) == (1 << 26) - 1;
}
}
import java.util.stream.Collectors;
public class Solution {
/**
* Time: O(n)
* Memory: O(1)
*/
public boolean checkIfPangram(String sentence) {
return sentence.chars().boxed().collect(Collectors.toSet()).size() == 26;
}
}