Posts

Showing posts with the label Data Structures

INTERVIEW BIT SOLUTION

 1) Rotate Matrix Interview Bit Solution using ArrayList public class Solution { public void rotate(ArrayList<ArrayList<Integer>> a) { int n = a.get(0).size(); for(int i=0;i<n/2;i++) { for(int j=i;j<n-i-1;j++) { int temp = a.get(i).get(j); int pp = a.get(n-j-1).get(i); a.get(i).set(j, pp); int oo = a.get(n-i-1).get(n-j-1); a.get(n-j-1).set(i, oo); int ll = a.get(j).get(n-i-1); a.get(n-i-1).set(n-j-1,ll); a.get(j).set(n-i-1,temp); } } } } Using array import java.util.Arrays; public class Rotate2DArrayInPlace { public static void main (String args [] ) throws Exception { Integer [] [] myNxNArray = new Integer [][] { {1, 2, 3}, {4,5,6}, {7,8,9} }; System.out.println("SOURCE ARRAY: \n" + Arrays.deepToString(myNxNArray)); Integer [][] rotatedInPlace = rotateMatrixInPlaceCounterClockwise(myNxNArray); System.out.println("ROTATED IN PLACE - 90 degrees COUNTER CLOCKWISE"); System.out.println(Arrays.deepToStrin...

permutation of a given string

Image
public class StringPermutation { public List< String > permute ( char input []) { Map< Character , Integer > countMap = new TreeMap<> (); for ( char ch : input) { countMap . compute(ch, (key, val) - > { if (val == null ) { return 1 ; } else { return val + 1 ; } }); } char str[] = new char [countMap . size()]; int count[] = new int [countMap . size()]; int index = 0 ; for ( Map . Entry< Character , Integer > entry : countMap . entrySet()) { str[index] = entry . getKey(); count[index] = entry . getValue(); index ++ ; } List< String > resultList = new ArrayList<> (); char result[] = new char [input . length]; permuteUtil(str, count, result, 0 , resultList); return resultList; } public void permuteUtil ( char str [], int count [], char result [], int level , List< String > resultList ) { if (level == result . length) { resultList . add( new String (re...

All Algorithms of Searching & Sorting in the Best & Easiest way.

SEARCHING  &  SORTING ALGORITHM Linear Search     for(start to end of array) { if (current_element equals to 5) { print (current_index); } }    Time Complexity: O(N) Space Complexity: O(1) Binary Search int binarySearch ( int low , int high , int key ) { while ( low <= high ) { int mid =( low + high )/ 2 ; if ( a [ mid ]< key ) { low = mid + 1 ; } else if ( a [ mid ]> key ) { high = mid - 1 ; } else { return mid ; } } return - 1 ; //key not found }     Time Complexity: O ( log 2 N )  Space Complexity: O(1) Bubble Sort void bubble_sort ( int A [ ], int n ) { int temp ; for ( int k = 0 ; k < n - 1 ; k ++) { // (n-k-1) is for ignoring comparisons of elements which have already been compared in earlier ite...