Popular Posts

Monday, February 13, 2012

Interleavings of two strings

Given two strings str1 and str2, write a function that prints all interleavings of the given two strings. You may assume that all characters in both strings are different

Example :
Input: str1 = "AB",  str2 = "CD"
Output:
    ABCD
    ACBD
    ACDB
    CABD
    CADB
    CDAB
public class InterLeavedStrings {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "def";

if (str1 != null && str1.length() != 0 && str2 != null
&& str2.length() != 0) {
getInterLeavedStrings(str1, str2);
getInterLeavedStrings(str2, str1);
} else if (str1 == null || str1.length() == 0) {
System.out.println(str2);
} else {
System.out.println(str1);
}
}

private static void getInterLeavedStrings(String str1, String str2) {
int m = str1.length();
int n = str2.length();
for (int i = 1; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.println(str1.substring(0, i) + str2.substring(0, j)
+ str1.substring(i, m) + str2.substring(j, n));
}
}
}
}


Another variant of interleaving : Check a given string is interleaving of other two strings
 http://www.geeksforgeeks.org/archives/17750


Refer : http://www.geeksforgeeks.org/archives/17743

Minimum number of jumps to reach last element


Problem: Given an array of integers, each element represents the max number of jumps can make forward. What is the minimum number of element selections to reach the end of the array (starting from the first element).
Example: arr = 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9
Here the min # of selections is : 3
with the sequence : 1-> 3 -> 8 ->9
First element is 1, so can only go to 3.
Second element is 3, so can make at most 3 jumps: eg to 5 or 8 or 9.
If an element is 0, then cannot make any jumps


public class MinNumberOfJumps {
private static int MAX = 23234;

public static void main(String[] args) {
int[] jumps = { 2, 3, 1, 1, 4 };
int minJumps = getMinJumps(jumps, jumps.length);

if (minJumps < MAX)
System.out
.println("Minimum number of jumps needed to reach the end of the array is : "
+ minJumps);
else
System.out.println("Cannot reach the end position of the array");
}

private static int getMinJumps(int[] jumps, int size) {
//Boundry conditions
if (jumps == null || size == 0 || jumps[0] == 0)
return MAX;

int[] dp = new int[size];
//Initialization of dp[i]
for (int i = 0; i < size; i++)
dp[i] = MAX;

dp[0] = 0;//no need to jump at first element

for (int i = 1; i < size; i++) {
for (int j = 0; j <= i; j++) {
if (jumps[j] >= (i - j)) {//check if jump is possible from j to is
if (dp[j] + 1 < dp[i]) //check if better solution available
dp[i] = dp[j] + 1;
}
}
}
return dp[size - 1];
}

}

Refer  : http://avidullu.wordpress.com/2011/01/12/dp-after-a-long-time/
http://tech-queries.blogspot.in/2011/08/find-optimal-number-of-jumps-to-reach.html

Tuesday, February 7, 2012

All logical bit questions

Linked list, Stack and queue questions

All array questions

Saturday, February 4, 2012

All Tree questions

Construct a Binary search tree

If you are given a number N, build a binary search tree of size N with node values from 1 to N.

public class BuildTree {

public Node getTree(int n) {
if (n <= 0) {
System.out
.println("Asking for constructing a tree of size zero or negative number.");
return null;
}
return buildTree(1, n);
}

public Node buildTree(int left, int right) {
if (left > right)
return null;

int rootValue = (int) Math.floor((left + right) / 2);
Node node = new Node(rootValue);
node.left = buildTree(left, rootValue - 1);
node.right = buildTree(rootValue + 1, right);
return node;
}

public static class Node {

public int value;
// member variables will be initialized to null by default.
public Node left;
public Node right;

public Node(int value) {
this.value = value;
}

public Node(int value, Node left, Node right) {
this.value = value;
this.left = left;
this.right = right;
}
}
}