Update appNew update is available. Click here to update.
About
Currently in 3 Year of Computer Engineering and on way to learning DSA in JAVA. For Support follow me on LinkedIn....
Sal Engineering And Technical Institute 2025
Java - Default language
My Stats
EXP gained
yellow-spark
44199
Level
7 (Expert)
Community stats
Discussions
0
Upvotes
1
Know more
Weekend contest rating
Contest attended
Problems solved
2021 2023
Better than %
Weekend contest rating
Contest attended
Problems solved
2021 2023
Better than %
1170
Total problems solved
1106
Easy
57
Moderate
7
Hard
0
Ninja
Jan Jan Feb Feb Mar Mar Apr Apr May May Jun Jun Jul Jul Aug Aug Sep Sep Oct Oct Nov Nov Dec Dec

Current streak:

8 days

Longest streak:

112 days

Less

More

Achievements
7
Ronin
Topics
Strings
Binary Search
Greedy
+ 4 more
3
Samurai
Topics
Linked List
Arrays
Sorting
Discussions
JAVA | ALL TEST CASES PASS
Interview problems

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;

    }

}

profile
sudhircode02
Published On 22-Oct-2023
24 views
0 replies
0 upvotes
JAVA 5 line Code | Beginner Approach
Interview problems

 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;

    }

profile
sudhircode02
Published On 22-Oct-2023
67 views
0 replies
0 upvotes
JAVA Beats 100% | Beginner Recursion Backtracking Approach
Interview problems

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;

    }

}

profile
sudhircode02
Published On 22-Oct-2023
43 views
0 replies
0 upvotes
JAVA | Beginner Friendly | Simplest Approach
Interview problems

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;

    }

}

profile
sudhircode02
Published On 18-Oct-2023
42 views
0 replies
0 upvotes
JAVA | Beginner Approach | Easy to Understand
Interview problems

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;

    }

 

profile
sudhircode02
Published On 18-Oct-2023
26 views
0 replies
0 upvotes
JAVA | Beats 90% | 3 line code
Interview problems

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));

            }

        }

    }

profile
sudhircode02
Published On 28-Sep-2023
50 views
0 replies
0 upvotes
JAVA | Easy to Understand | Simplest
Interview problems

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;

    }

profile
sudhircode02
Published On 20-Sep-2023
65 views
0 replies
1 upvotes
JAVA | Easiest | Beats 100% | Simplest
Interview problems

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;

    }

profile
sudhircode02
Published On 20-Sep-2023
84 views
0 replies
0 upvotes
JAVA | Easy to Understand | Simplest
Interview problems

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;

    }

profile
sudhircode02
Published On 15-Sep-2023
118 views
0 replies
0 upvotes
JAVA | 3 line code
Interview problems

public static void swapNumber(int []a, int []b) {

        // Write your code here.

        int c = a[0];

        a[0] = b[0];

        b[0] = c;

        

    }

profile
sudhircode02
Published On 15-Sep-2023
263 views
0 replies
0 upvotes