Showing posts with label Java Programming. Show all posts
Showing posts with label Java Programming. Show all posts

Friday, August 26, 2016

GeeksForGeeks Print all combinations of balanced parentheses

Question reference
http://www.geeksforgeeks.org/print-all-combinations-of-balanced-parentheses/
Write a function to generate all possible n pairs of balanced parentheses. 
For example, if n=1
{}
for n=2
{}{}
{{}}

I understood implementation from below  references
http://www.geeksforgeeks.org/print-all-combinations-of-balanced-parentheses/
https://discuss.leetcode.com/topic/8724/easy-to-understand-java-backtracking-solution/2
Solution in Java
View point is that the no of "(" is equal to ")"


package generate_all_paranthesis;

import java.util.*;

public class Solution1 {
public ArrayList<String> generateParenthesis(int a
{
    ArrayList<String> res = new ArrayList<String>();
    if(a==0)
    {
    return res;
    }    
    backtracking(0,0,a,new StringBuilder(),res);
    return res;
}
public static void backtracking(int open,int close,int a,StringBuilder temp,ArrayList<String> res)
{
if(close==a)
{
res.add(temp.toString());
return;
}
else
{
           //go on adding ")" if open > close
if(open>close)
{
                                //appending ")"
                temp.append(")");
backtracking(open, close+1, a, temp, res);
temp.setLength(temp.length()-1);
                                //removing ")"

}
           //go on adding "(" if open <n
if(open<a)
{
                                //appending "("
                                temp.append("(");
backtracking(open+1, close, a, temp, res);
temp.setLength(temp.length()-1);
                                //removing "("
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
      Solution1 s1 = new Solution1();
      System.out.println(s1.generateParenthesis(3));
}

}





GeeksForGeeks Print Martix in Spiral Order


Print a given matrix in spiral form

Given a 2D array, print it in spiral form. See the following examples.
Input:
        1    2   3   4
        5    6   7   8
        9   10  11  12
        13  14  15  16
Output: 
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 


Input:
        1   2   3   4  5   6
        7   8   9  10  11  12
        13  14  15 16  17  18
Output: 
1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11

Below is the clear link for explanation and pseudo code implementation


https://www.youtube.com/watch?v=siKFOI8PNKM&feature=youtu.be

Solution in Java

public class Solution {
// DO NOT MODIFY THE LIST
public ArrayList<Integer> spiralOrder(final List<ArrayList<Integer>> a) {

                //Initialization of 4 pointers top bottom left and right
int top = 0,bottom = a.size()-1,left = 0,right = a.get(0).size()-1;
                // below variable will control the direction
int direction = 0;
ArrayList<Integer> res = new ArrayList<Integer>();
               // the while loop will execute until the top meets bottom and left meets right
while(bottom>=top && right>=left)
{
   if(direction==0)
   {
       for(int i =left;i<=right;i++)
       {
         
           res.add(a.get(top).get(i));
       }
     
       top++;
   }
   else if(direction==1)
   {
       for(int i =top;i<=bottom;i++)
       {
           res.add(a.get(i).get(right));
       }
     
       right--;
   }
   else if(direction==2)
   {
       for(int i=right;i>=left;i--)
       {
           res.add(a.get(bottom).get(i));
       }
     
       bottom--;
   }
   else if(direction==3)
   {
       for(int i =bottom;i>=top;i--)
       {
           res.add(a.get(i).get(left));
       }
     
       left++;
   }
                   //this helps to put back the direction to 0 so that we are again pointed to the top row of the
                   //matrix
   direction = (direction+1)%4;
}
   

return res;
}
}