Strings - Backspace String Compare using JAVA Stack

Question: Given two Strings S and T, return true if they equal when both are typed out any "#" will be treated as backspace. input - S -> ab#c, T -> az#c output - True Constraints need to ask because solution may change according to given constraints Question: What happens when 2 or multiple "#" appear besides each other? Answer: Delete same number of values before 1st "#". Ex- ab## -> "" Question: What happen to "#" when there is no character to remove? Answer: It deletes nothing. Ex- a###b -> "b" Question: Are two empty Strings equals each other? Answer: Ex- S -> a#b#c#, T -> c#. Both returns empty string and output will be true. Question: Does case sensitivity matters? Answer: Yes. Ex- A is different from a. S -> ab#c, T -> Az#c. Output - False Solution using JAVA language. BruteForce Approach - This is simple approach, We are using JAVA Stack here,...

Java Multi-Dimensional Array

 Array - In computer science, an array is a collection of objects with same data type or we can say array as a container that can hold a fixed number of objects. Example: Below we can see an array containing 7 integer values and index is starting from 0.

array, array index, array data storage
Image a: Array Representation

Properties of array:

  • Array is simplest, most used data structure and used for implementing other data structures as list, stacks, queues etc.
  • Data type must be same.
  • Length is fixed.
  • Length must be specified during array initialization.
  • Each object stored at an index.
  • Array index starts from zero.
  • Object can be retrieved directly by index number.
Types of array:
  • One-dimensional array - 1-D or single dimensional array is a linear array, they can represent any row or column.
    Example : int arr = new int[5];
  • Multi-dimensional array - Multi-Dimensional array is an array of array like 2-D array, 3-D array. Each element of multi-dimensional array is an array. Below we have an example of 2-D array in JAVA which can hold 9 elements, a 2D array holds rows and columns both and index always starts from 0. lets see:
    Example: int[][] a = new int[3][3];

    multi-dimensional array, 2-D array
    Image b: 2-D Array
  • Java Code for 2-D array:
    
    public class TwoDimensionalArray {
        public static void main(String[] args) {
            /*
            Declaration and initialization of array
            int [][] array;
            int[][] a = new int[3][3];
            int[][] b = new int[][]{
                {3, 3, 5},
                {2, 6, 1},
                {5, 7, 8},
            };*/
            int[][] arr = {
                    {3, 3, 5},
                    {2, 6, 1},
                    {5, 7, 8},
            };
            /*
            * printing multi dimensional array with old technique
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr[i].length; j++) {
                    System.out.print(arr[i][j] + " ");
                }
                System.out.print(" \n");
            }
            */
            /* Printing multi dimensional array with for...each loop */
            for (int[] innerArray : arr) {
                for (int data : innerArray) {
                    System.out.print(data + " ");
                }
                System.out.print(" \n");
            }
        }
    }
    
    Output:
    
    3 3 5  
    2 6 1  
    5 7 8  
  • Java Code for 3-D array:
    
    public class ThreeDimensionalArray {
        public static void main(String[] args) {
            /*
            Declaration and initialization of array
            int [][][] array;
            int[][][] a = new int[3][3][3]; // total 27 elements can be stored
            int[][][] b = new int[][][]{
                    {
                            {3, 3, 5},
                            {2, 6, 1}
                    },
                    {
                            {-3, -3, -5},
                            {-2, -6, -1}
                    }
            };*/
            int[][][] arr = {
                    {
                            {3, 3, 5},
                            {2, 6, 1},
                            {8, 4, 7}
                    },
                    {
                            {-3, -3, -5},
                            {-2, -6, -1},
                            {-8, -4, -7}
                    },
                    {
                            {3, 3, 5},
                            {2, 6, 1},
                            {8, 4, 7}
                    }
            };
            /** printing multi dimensional array with old technique
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr[i].length; j++) {
                    for (int k = 0; k < arr[i].length; k++) {
                        System.out.print(arr[i][j][k] + " ");
                    }
                    System.out.print(" \n");
                }
                System.out.print(" \n");
            }*/
            /* Printing multi dimensional array with for-each loop */
            for (int[][] outerArray : arr) {
                for (int[] innerArray : outerArray) {
                    for (int data : innerArray) {
                        System.out.print(data + " ");
                    }
                    System.out.print(" \n");
                }
                System.out.print(" \n");
            }
        }
    }
    
    Output:
    
    3 3 5  
    2 6 1  
    8 4 7  
     
    -3 -3 -5  
    -2 -6 -1  
    -8 -4 -7  
     
    3 3 5  
    2 6 1  
    8 4 7  
Most Important Interview Questions of Array- 

Comments

Popular posts from this blog

Arrays - Trapping Rainwater - Optimal Solution using 2 Pointer Approach

Arrays - Container with Most Water

Strings - Backspace String Compare using JAVA Stack