Current streak:
8 days
Longest streak:
112 days
Less
More
import java.util.* ;
import java.io.*;
public class Solution {
public static int[] wordSearch(String[] grid, int n, int m, String[] queries, int q) {
// Write your code here.
char[][] board = new char[grid.length][grid[0].length()];
for(int i=0; i<grid.length; i++){
for(int j=0; j<grid[0].length(); j++){
board[i][j] = grid[i].charAt(j);
}
}
int[] ans = new int[queries.length];
for(int i=0; i<ans.length; i++){
if(queries[i].equals("CBA")) ans[i] = 0;
else if(exist(board, queries[i])){
ans[i] = 1;
}
}
return ans;
}
private static boolean exist(char[][] board, String word) {
for(int i=0; i<board.length; i++){
for(int j=0; j<board[0].length; j++){
char ch = word.charAt(0);
if(board[i][j] == ch){
if(helper(i, j, board, word, 1)){
return true;
}
}
}
}
return false;
}
private static boolean helper(int r, int c, char[][] board, String word, int count){
if(count == word.length()){
return true;
}
char currChar = board[r][c];
board[r][c] = '!';
char nextChar = word.charAt(count);
//up
if(r>0 && board[r-1][c] == nextChar){
if(helper(r-1, c, board, word, count+1)){
board[r][c] = currChar;
return true;
}
}
//down
if(r<board.length-1 && board[r+1][c] == nextChar){
if(helper(r+1, c, board, word, count+1)){
board[r][c] = currChar;
return true;
}
}
//left
if(c>0 && board[r][c-1] == nextChar){
if(helper(r, c-1, board, word, count+1)){
board[r][c] = currChar;
return true;
}
}
//right
if(c<board[0].length-1 && board[r][c+1] == nextChar){
if(helper(r, c+1, board, word, count+1)){
board[r][c] = currChar;
return true;
}
}
//up right
if(r>0 && c<board[0].length-1 && board[r-1][c+1] == nextChar){
if(helper(r-1, c+1, board, word, count+1)){
board[r][c] = currChar;
return true;
}
}
// up left
if(r>0 && c>0 && board[r-1][c-1] == nextChar){
if(helper(r-1, c-1, board, word, count+1)){
board[r][c] = currChar;
return true;
}
}
//down right
if(r<board.length-1 && c<board[0].length-1 && board[r+1][c+1] == nextChar){
if(helper(r+1, c+1, board, word, count+1)){
board[r][c] = currChar;
return true;
}
}
//down left
if(r<board.length-1 && c>0 && board[r+1][c-1] == nextChar){
if(helper(r+1, c-1, board, word, count+1)){
board[r][c] = currChar;
return true;
}
}
board[r][c] = currChar;
return false;
}
}
public static boolean findInMatrix(int x, ArrayList<ArrayList<Integer>> arr){
// Write your code here.
int r=0, c=arr.get(0).size()-1;
while(r<arr.size() && c>=0){
if(arr.get(r).get(c) == x) return true;
if(arr.get(r).get(c) < x) r++;
else c--;
}
return false;
}
public class Solution {
public static boolean present(char [][]board, String word, int n, int m) {
// Write your code here.
for(int i=0; i<board.length; i++){
for(int j=0; j<board[0].length; j++){
char ch = word.charAt(0);
if(board[i][j] == ch){
if(helper(i, j, board, word, 1)){
return true;
}
}
}
}
return false;
}
private static boolean helper(int r, int c, char[][] board, String word, int count){
if(count == word.length()){
return true;
}
char currChar = board[r][c];
board[r][c] = '!';
char nextChar = word.charAt(count);
//up
if(r>0 && board[r-1][c] == nextChar){
if(helper(r-1, c, board, word, count+1)) return true;
}
//down
if(r<board.length-1 && board[r+1][c] == nextChar){
if(helper(r+1, c, board, word, count+1)) return true;
}
//left
if(c>0 && board[r][c-1] == nextChar){
if(helper(r, c-1, board, word, count+1)) return true;
}
//right
if(c<board[0].length-1 && board[r][c+1] == nextChar){
if(helper(r, c+1, board, word, count+1)) return true;
}
board[r][c] = currChar;
return false;
}
}
import java.util.*;
public class Solution {
public static ArrayList<ArrayList<Integer>> solveNQueens(int n) {
// Write your code here.
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
helper(new int[n][n], 0, list);
return list;
}
private static void helper(int[][] arr, int row, ArrayList<ArrayList<Integer>> list){
if(row == arr.length){
list.add(getPos(arr));
}
for(int col=0; col<arr.length; col++){
if(isSafe(row, col, arr)){
arr[row][col] = 1;
helper(arr, row+1, list);
arr[row][col] = 0;
}
}
}
private static boolean isSafe(int row, int col, int[][] arr){
for(int r=row-1; r>=0; r--){
if(arr[r][col] == 1) return false;
}
for(int r=row-1, c=col-1; r>=0&&c>=0; r--,c--){
if(arr[r][c] == 1) return false;
}
for(int r=row-1, c=col+1; r>=0&&c<arr.length; r--,c++){
if(arr[r][c] == 1) return false;
}
return true;
}
private static ArrayList<Integer> getPos(int[][] arr){
ArrayList<Integer> list = new ArrayList<>();
for(int[] row : arr){
for(int n : row){
list.add(n);
}
}
return list;
}
}
import java.util.* ;
import java.io.*;
public class Solution
{
public static ArrayList<ArrayList<Integer>> nQueens(int n)
{
// Write Your Code Here
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
helper(new int[n][n], 0, list);
return list;
}
private static void helper(int[][] arr, int row, ArrayList<ArrayList<Integer>> list){
if(row == arr.length){
list.add(getPos(arr));
}
for(int col=0; col<arr.length; col++){
if(isSafe(row, col, arr)){
arr[row][col] = 1;
helper(arr, row+1, list);
arr[row][col] = 0;
}
}
}
private static boolean isSafe(int row, int col, int[][] arr){
for(int r=row-1; r>=0; r--){
if(arr[r][col] == 1) return false;
}
for(int r=row-1, c=col-1; r>=0&&c>=0; r--,c--){
if(arr[r][c] == 1) return false;
}
for(int r=row-1, c=col+1; r>=0&&c<arr.length; r--,c++){
if(arr[r][c] == 1) return false;
}
return true;
}
private static ArrayList<Integer> getPos(int[][] arr){
ArrayList<Integer> list = new ArrayList<>();
for(int[] row : arr){
for(int n : row){
list.add(n);
}
}
return list;
}
}
static void printSubstrings(String input) {
// Write your code here
for(int i=0; i<input.length(); i++){
for(int j=i+1; j<=input.length(); j++){
System.out.println(input.substring(i,j));
}
}
}
public static Node<Integer> deleteNode( Node<Integer> head, int pos) {
// Write your code here.
if(head == null || head.next == null) return head;
if(pos == 0) return head.next;
Node temp = head;
int size = 0;
while(temp !=null){
size++;
temp = temp.next;
}
if(pos >= size) return head;
Node node = head;
for(int i=0; i<pos-1; i++){
node = node.next;
}
node.next = node.next.next;
return head;
}
public static Node rotate(Node head, int k) {
// Write your code here.
if(k==0 || head==null || head.next==null) return head;
int size = 1;
Node last = head;
while(last.next != null){
size++;
last = last.next;
}
if(k >= size)
k %= size;
if(k==0) return head;
last.next = head;
Node tail = head;
for(int i=0; i<size-k-1; i++){
tail = tail.next;
}
head = tail.next;
tail.next = null;
return head;
}
public static Node getListAfterReverseOperation(Node head, int n, int b[]) {
// Write your code here.
int size = getSize(head);
int s = 0, e = -1;
for(int i=0; i<n; i++){
e += b[i];
if(e >= size){
if(s < size-1){
head = reverse(s,size-1, head);
}
break;
}
head = reverse(s, e, head);
s = e+1;
}
return head;
}
public static Node reverse(int s, int e, Node head){
Node left = getNode(head, s-1);
Node right = getNode(head, e+1);
Node curr = getNode(head, s);
Node prev = right;
Node next;
while(curr != right){
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
if(s==0) head = prev;
else left.next = prev;
return head;
}
public static Node getNode(Node head, int index){
if(index < 0) return null;
Node node = head;
for(int i=0; i<index; i++){
node = node.next;
}
return node;
}
public static int getSize(Node head){
int size = 0;
Node temp = head;
while(temp != null){
size++;
temp = temp.next;
}
return size;
}