LeetCode Weekly Contest 483 Discussion and Solution
Priyanshu Chaurasiya
26 days ago
Priyanshu Chaurasiya
26 days ago

Join the conversation by signing in below!
| Problem | Difficulty | Points | Status |
|---|---|---|---|
| 1 | Easy | 3 | Solved |
| 2 | Medium | 4 | Solved |
| 3 | Medium | 5 | Unsolved |
| 4 | Hard | 6 | Unsolved |
class Solution {
public String largestEven(String s) {
int n = s.length();
int i = n - 1;
while(i >= 0 && s.charAt(i) == '1'){
i--;
}
StringBuilder ans = new StringBuilder();
for(int j = 0; j <= i; j++){
ans.append(s.charAt(j));
}
return ans.toString();
}
}class Solution {
public boolean isValid(List<String> list){
String top = list.get(0);
String left = list.get(1);
String right = list.get(2);
String bottom = list.get(3);
if(top.charAt(0) == left.charAt(0) && top.charAt(3) == right.charAt(0) && bottom.charAt(0) == left.charAt(3) && bottom.charAt(3) == right.charAt(3)){
return true;
}
return false;
}
public void permute(String[] strs, int idx, List<List<String>> comb){
if(idx == strs.length){
comb.add(new ArrayList<>(Arrays.asList(strs)));
return;
}
for(int i = idx; i < strs.length; i++){
swap(strs, idx, i);
permute(strs, idx + 1, comb);
swap(strs, idx, i);
}
}
public void swap(String[] strs, int i, int j){
String temp = strs[i];
strs[i] = strs[j];
strs[j] = temp;
}
public List<List<String>> wordSquares(String[] words) {
int n = words.length;
List<List<String>> ans = new ArrayList<>();
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
for(int k = j + 1; k < n; k++){
for(int l = k + 1; l < n; l++){
String[] strs = {words[i], words[j], words[k], words[l]};
List<List<String>> comb = new ArrayList<>();
permute(strs, 0, comb);
for(int x = 0; x < comb.size(); x++){
if(isValid(comb.get(x))){
ans.add(comb.get(x));
}
}
}
}
}
}
Collections.sort(ans, (a, b) -> a.get(0).compareTo(b.get(0)) != 0 ? a.get(0).compareTo(b.get(0)) :
a.get(1).compareTo(b.get(1)) != 0 ? a.get(1).compareTo(b.get(1)) :
a.get(2).compareTo(b.get(2)) != 0 ? a.get(2).compareTo(b.get(2)) :
a.get(3).compareTo(b.get(3)));
return ans;
}
}No comments yet, Start the conversation!