Skip to content

Commit 780b22d

Browse files
committed
Improved task
1 parent 0dfc052 commit 780b22d

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

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

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

3-
// #Easy #String #2024_05_07_Time_1_ms_(99.39%)_Space_41.9_MB_(59.69%)
3+
// #Easy #String #2025_07_15_Time_1_ms_(99.12%)_Space_42.10_MB_(62.25%)
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-
};
1910
boolean hasVowel = false;
2011
boolean hasConsonant = 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;
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;
3122
}
3223
}
3324
return hasVowel && hasConsonant;

0 commit comments

Comments
 (0)