Skip to content

Commit 0dfc052

Browse files
committed
Improved task 3136
1 parent a55ce6e commit 0dfc052

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/main/java/g3101_3200/s3136_valid_word/Solution.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
package g3101_3200.s3136_valid_word;
22

3-
// #Easy #String #2025_07_15_Time_1_ms_(99.12%)_Space_42.10_MB_(62.25%)
3+
// #Easy #String #2024_05_07_Time_1_ms_(99.39%)_Space_41.9_MB_(59.69%)
44

55
public class Solution {
66
public boolean isValid(String word) {
77
if (word.length() < 3) {
88
return false;
99
}
10+
if (word.contains("@") || word.contains("#") || word.contains("$")) {
11+
return false;
12+
}
13+
char[] vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
14+
char[] consonants = {
15+
'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v',
16+
'w', 'x', 'y', 'z', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q',
17+
'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z'
18+
};
1019
boolean hasVowel = false;
1120
boolean hasConsonant = false;
12-
for (char c : word.toCharArray()) {
13-
if (Character.isLetter(c)) {
14-
char ch = Character.toLowerCase(c);
15-
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
16-
hasVowel = true;
17-
} else {
18-
hasConsonant = true;
19-
}
20-
} else if (!Character.isDigit(c)) {
21-
return false;
21+
for (char c : vowels) {
22+
if (word.indexOf(c) != -1) {
23+
hasVowel = true;
24+
break;
25+
}
26+
}
27+
for (char c : consonants) {
28+
if (word.indexOf(c) != -1) {
29+
hasConsonant = true;
30+
break;
2231
}
2332
}
2433
return hasVowel && hasConsonant;

0 commit comments

Comments
 (0)