Skip to content

Commit 7529944

Browse files
solves #3142: Check if Grid Satisfies Conditions in java
1 parent 1a47bf1 commit 7529944

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,8 @@
908908
| 3120 | [Count the Number of Special Characters I](https://leetcode.com/problems/count-the-number-of-special-characters-i) | [![Java](assets/java.png)](src/CountTheNumberOfSpecialCharactersI.java) | |
909909
| 3127 | [Make a Square with the Same Color](https://leetcode.com/problems/make-a-square-with-the-same-color) | [![Java](assets/java.png)](src/MakeASquareWithTheSameColor.java) | |
910910
| 3131 | [Find the Integer Added to Array I](https://leetcode.com/problems/find-the-integer-added-to-array-i) | [![Java](assets/java.png)](src/FindTheIntegerAddedToArrayI.java) | |
911-
| 3136 | [Valid Word](https://leetcode.com/problems/valid-word) | | |
912-
| 3142 | [Check if Grid Satisfies Conditions](https://leetcode.com/problems/check-if-grid-satisfies-conditions) | | |
911+
| 3136 | [Valid Word](https://leetcode.com/problems/valid-word) | [![Java](assets/java.png)](src/ValidWord.java) | |
912+
| 3142 | [Check if Grid Satisfies Conditions](https://leetcode.com/problems/check-if-grid-satisfies-conditions) | [![Java](assets/java.png)](src/CheckIfGridSatisfiesConditions.java) | |
913913
| 3146 | [Permutation Difference between Two Strings](https://leetcode.com/problems/permutation-difference-between-two-strings) | | |
914914
| 3151 | [Special Array I](https://leetcode.com/problems/special-array-i) | | |
915915
| 3158 | [Find the XOR of Numbers Which Appear Twice](https://leetcode.com/problems/find-the-xor-of-numbers-which-appear-twice) | | |

Diff for: src/CheckIfGridSatisfiesConditions.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// https://leetcode.com/problems/check-if-grid-satisfies-conditions
2+
// T: O(m * n)
3+
// S: O(1)
4+
5+
public class CheckIfGridSatisfiesConditions {
6+
public boolean satisfiesConditions(int[][] grid) {
7+
for (int i = 0 ; i < grid.length ; i++) {
8+
for (int j = 0 ; j < grid[i].length ; j++) {
9+
if (j + 1 < grid[i].length && grid[i][j] == grid[i][j + 1]) {
10+
return false;
11+
}
12+
if (i + 1 < grid.length && grid[i][j] != grid[i + 1][j]) {
13+
return false;
14+
}
15+
}
16+
}
17+
return true;
18+
}
19+
}

Diff for: src/ValidWord.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// https://leetcode.com/problems/valid-word
2+
// T: O(N)
3+
// S: O(1)
4+
5+
import java.util.Set;
6+
7+
public class ValidWord {
8+
private static final Set<Character> VOWELS = Set.of('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U');
9+
10+
public boolean isValid(String word) {
11+
return word.length() >= 3 && isAlphaNum(word) && hasOneVowel(word) && hasOneConsonant(word);
12+
}
13+
14+
private static boolean isAlphaNum(String s) {
15+
for (int i = 0 ; i < s.length() ; i++) {
16+
if (!Character.isAlphabetic(s.charAt(i)) && !Character.isDigit(s.charAt(i))) {
17+
return false;
18+
}
19+
}
20+
return true;
21+
}
22+
23+
private static boolean hasOneVowel(String s) {
24+
for (int i = 0 ; i < s.length() ; i++) {
25+
if (VOWELS.contains(s.charAt(i))) {
26+
return true;
27+
}
28+
}
29+
return false;
30+
}
31+
32+
private static boolean hasOneConsonant(String s) {
33+
for (int i = 0 ; i < s.length() ; i++) {
34+
if (Character.isAlphabetic(s.charAt(i)) && !VOWELS.contains(s.charAt(i))) {
35+
return true;
36+
}
37+
}
38+
return false;
39+
}
40+
}

0 commit comments

Comments
 (0)