Wednesday, 1 April 2020

Tricky Programs

I am publishing my coding thing into git
https://github.com/rameshvanka/coding/tree/master/optumtest


I am publishing tricky java program questions for students. It will ongoing process.

Find the Duplicate Elements In a String
public class Unique {
public static void main(String args[]){
String ram = "ramesh";
boolean[] char_val = new boolean[256];
for(int i=0; i < ram.length();i++){
int val = ram.charAt(i);
if(char_val[val]){
System.out.println("Duplication Element:"+ram.charAt(i));
}
char_val[val] = true;
}
}

}

Here Tricky thing having the boolean array of 256 ( 8 - bits, 2 power 8 = 256) and make them true concerned array cell.

Palindromic Substrings:
i/p: mokkori,
palindrome substrings: m, o, k, i, r, kk, okko  

Main logic:  find the list of substrings and check each substring palindrome or not.
i=0, str[i] = m  --> m, mo, mok, mokk, mokko, mokkor, mokkori
i=1, str[i] = o   --> o, ok, okk, okko, okkor, okkori
i=2, str[i] = k   --> k, kk, kko, kkor, kkori
i=3, str[i] = k   --> k, ko, kor, kori
i=4, str[i] = o   --> o, or, ori
i=5, str[i] = r   -->  r, ri
i=6, str[i] = i   -->  i

Java Logic:
      split substring logic:  for (int i =0 ; i < str.length(); i++) {
                                           String temp = "";
                                           for(int j=i, j < str.length(); j++ ) {
                                                   temp += str.charAt(j);
                                                    //Check Palindrome of temp and get status.
public class PalindromSubString {
static Set<String> palSet = new HashSet<String>();
public static int countPalindrome(String str) {
for(int i=0; i < str.length(); i++) {
String temp = "";
for (int j=i; j < str.length(); j++) {
temp += str.charAt(j);
boolean status = checkBoolean(temp);
if(status) {
palSet.add(temp);
}
System.out.println(temp);
}
}
for(String s : palSet) {
System.out.print(s);
System.out.print(" ");
}
return palSet.size();

}

public static boolean checkBoolean(String str) {
char[] charStr = str.toCharArray();
int i = 0;
int j = str.length()-1;
System.out.println("String temp:"+str);
while(i < j) {
if(charStr[i] != charStr[j]) {
return false;
}
i++;
j--;
}
return true;

}
}
  
Valid BST Tree: Given Binary Tree is valid BST (Binary Search Tree) Tree or Not ?
In the BST Tree all elements are in sorted order. 
Main Logic: BST  Tree Inorder Traversal will go through sorted order means ( left, node, right). I am using inorder traversal checking leftnode value < node value < rightnode value, if condition fails it's not valid BST. I am using recursion inorder traversal, recursion means nothing but stack, in stack all things are local scope only. hence I am storing previous value in the instance variable.

//My simple technique, Inorder traversal gives sorted tree, then checking previous < root < right
int prevItemValue = 0;
public  boolean validBST(TreeNode root) {
if(root == null) {
return true;
}
return validBST(root.left) && checkMin(root) && validBST(root.right);
}

public  boolean checkMin(TreeNode root) {
System.out.println("prevItemValue :" + prevItemValue  + " CurrItemValue:" + root.item);
if(prevItemValue <= root.item) {
prevItemValue = root.item;
//System.out.println(root.item + "item:" + min);
} else {
return false;
}
return true;

}
return validBST(root.left) && checkMin(root) && validBST(root.right); Why I am returning like this means return true&&true&&true&&true&&true&&false&&true; here if any false will give false due to && condition.














No comments:

Post a Comment