Skip to main content

Posts

Showing posts from September, 2015

Function in java to check whether given string is PALINDROME or Not.

File:Execute.java --------------------------------------------------------------------------------- package org.developersbrain.solutions; public class Execute { public static boolean isPalindrome(String str){ char[] chrArr=str.toCharArray(); int len=chrArr.length; boolean palindrome=true; for(int i=0,j=len-1;i<=len/2;i++,j--){ if(chrArr[i]!=chrArr[j]){ palindrome=false; } } return palindrome; } public static void main(String[] args){ String[] str={"MADAM","CIVIC","SINGLE","DONKEY","RADAR"}; for(String strV:str){ if(isPalindrome(strV)){ System.out.println("String \""+strV+"\" is palindrome."); }else{ System.out.println("String \""+strV+"\" is not palindrome."); } } } } Output: -------------------------------------------------------------------------------- String "MADAM" is palindrome

How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum element? Push,pop and min should all operate in O(1) time.

File:StackBox.java --------------------------------------------------------------------------------------------------- package org.developersbrain.solutions; class Student{ String studentName; int   totalMarks; String grade; Student(String studentName,int totalMarks,String grade){ this.studentName = studentName; this.totalMarks  = totalMarks; this.grade       = grade; } public int hashCode(){ return totalMarks; } public String toString(){ System.out.println("Student Name :"+this.studentName); System.out.println("Total Marks :"+this.totalMarks); System.out.println("Grade :"+this.grade); System.out.println(); System.out.println("-------------------------------------------"); return ""; } } class StackNode<ELEMENT>{ StackNode<ELEMENT> next; ELEMENT e;     ELEMENT min; StackNode(ELEMENT e){ this.e = e; next = null; } } pu

Minimum/ Maximum from the given list

File: Execute.java -------------------------------------------------------------------------------------- package org.developersbrain.Solutions; public class Execute { public static int min(int[] arrList){ int minV1=0; int minV2=0; int aLen=arrList.length; minV1=arrList[0]; minV2=arrList[aLen-1]; for(int i=0,j=aLen-1;i&lt;=aLen/2;i++,j--){ if(minV1 &gt; arrList[i]){ minV1=arrList[i]; } if(minV2 &gt; arrList[j]){ minV2=arrList[j]; } } if(minV2&lt;=minV1){ return minV2; }else{ return minV1; } } public static int max(int[] arrList){ int minV1=0; int minV2=0; int aLen=arrList.length; minV1=arrList[0]; minV2=arrList[aLen-1]; for(int i=0,j=aLen-1;i&lt;=aLen/2;i++,j--){ if(minV1 &lt; arrList[i]){ minV1=arrList[i]; } if(minV2 &lt; arrList[j]){ minV2=arrList[j]; } } if(minV2&gt;=minV1){ return minV2; }else{ return minV1; } }

Simple TREE data structure implementation in Java ( Without Recursions )

File : Tree.java ---------------------------------------------------------------------------- package org.developersbrain.solutions; class BinaryNode{ BinaryNode left; BinaryNode right; Integer t; BinaryNode(Integer t){ this.t = t; left   = null; right = null; } } public class Tree { BinaryNode root; Tree(){ root = null; } void add(Integer t){ BinaryNode bn=new BinaryNode(t); if(root==null){ root = bn; }else{ BinaryNode node=root; BinaryNode tmp=null; while(node!=null){ tmp = node; if(node.t <= bn.t){ node=node.right; }else{ node=node.left; } } if(tmp.t<=bn.t){ tmp.right=bn; }else{ tmp.left=bn; } } } void traverse(){ BinaryNode parent=root; BinaryNode child=root.left; while(child!=null){ if(parent!=null){ parent.left=child.right; child.right=parent; } if(child.left!=null){ parent = child; child=c

Doubly Linked List Implementation in Java

File: LinkedList.java ------------------------------------------------------------------------------------------------ package org.developersbrain.solutions; class Block<T>{ Block<T> prevNode; Block<T> nextNode; T t; Block(T t){ this.t = t; prevNode=null; nextNode=null; } } public class LinkedList<T> { Block<T> tail; Block<T> head; LinkedList(){ head=null; tail=null; } void add(T t){ Block<T> newBlock=new Block<T>(t); if(head==null){ head=newBlock; tail=newBlock; }else{ if(tail.prevNode==null){ head=newBlock; head.nextNode=tail; tail.prevNode=head; }else{ newBlock.nextNode=head; head.prevNode=newBlock; head=newBlock; } } } void deleteNode(T t){ Block<T> nodes=head; while(nodes!=null){ if(nodes.t.equals(t)){ if(nodes.prevNode==null){ head=nodes.nextNode; head.p

Given a set of coin denominators, find the minimum number of coins to give a certain amount of change.

File: Denominator.java ------------------------------------------------------------- package org.developersbrain.solutions; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class Denominator { String currency; Properties prop; InputStream inS; Denominator(){ currency = "INR";//Default Currency prop     = null; } Denominator(String currency) throws IOException{ this.currency = currency; prop = new Properties(); inS  = new FileInputStream("DenominatorSpec.properties"); prop.load(inS); } String returnDenominations(int amount){ String strDenom=""; String strAmount=""+amount; int    denom = 0,denomOther=0,tmpDenom=0; String denomValue; int d=0; int m=1; for(int i=0,j=1;i<strAmount.length();i++){ denom = amount%10; denomValue=prop.getProperty(currency+"."+m);

Implement a simple STACK DS in java

File: Stack.java ------------------------------------------------------------------ package org.developersbrain.solutions; class DataNode<T>{ T t; DataNode<T> next; DataNode(T t){ this.t=t; } } public class Stack<T> { DataNode<T> top; Stack(){ top=null; } protected void push(T t){ DataNode<T> newN=new DataNode<T>(t); if(top==null){ top=newN; }else{ newN.next=top; top=newN; } } private DataNode<T> pop(){ if(top==null) return null; DataNode<T> popNode=top; top=top.next; return popNode; } protected void popElement(){ DataNode<T> data=pop(); System.out.println(data.t); } protected void printStack(){ DataNode<T> printer=top; while(printer!=null){ System.out.println(printer.t); printer=printer.next; } } } File:MainClass.java ---------------------------------------------- package org.developersbrain.solutions; pu

Write a function that reverses the order of the words in a string.

Write a function that reverses the order of the words in a string. For Example, your function should transform the string "Do or do not, there is no try." to "try. no is there not, do or Do". Assume that all words are space delimited and treat punctuation same as letters. Filename: Execute.java ---------------------------------------------------------------------------------------------------- package org.developersbrain.solutions; public class Execute { public static String reverseWords(String str,char delimiter){ //Split the words into array of strings char[] chrArray=str.toCharArray(); int no_of_Delimiters=0; for(int i=0;i<chrArray.length;i++){ if(chrArray[i]==delimiter) no_of_Delimiters++; } //Populate string list of words String[] strList=new String[no_of_Delimiters+1]; for(int i=0,j=0;j<chrArray.length;j++){ if(i==0 && j==0){ strList[i]=""; } if(chrArray[j]==delimiter

You are given two sorted arrays, A and B and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order.

File:ArrayOperations.java ---------------------------------------------------------- package org.developersbrain.solutions; import java.util.Arrays; public class ArrayOperations { static int returnEmptyElements(int[] arrA){ int zeroElement=0,nonZeroElement = 0; for(int i=0;i<arrA.length;i++){ if(arrA[i]==0){ zeroElement=i; }else{ nonZeroElement=i; } } return nonZeroElement+1; } //Straightforward method - Append the array in Array 1 public static int[] mergeArray(int[] arrA,int[] arrB){ int i=returnEmptyElements(arrA); int[] arrCopy=new int[(i+arrB.length)]; if((i+arrB.length)>arrA.length){ System.out.println("Array cannot be merged due to size limit in Array 1."); return arrA; }else{ int j; for(j=0;j<i;j++){ arrCopy[j]=arrA[j]; } for(int k=0;k<arrB.length;k++,j++){ arrCopy[j] = arrB[k]; } } return arrCopy; } public static void main(String[] args){ i

Write code to reverse the given string

File 1: StringRoutines.java -------------------------------------------------------------- package org.developersbrain.solutions; public class StringRoutines { int getArrayLength(char[] arrList){ return arrList.length; } String convertArrString(char[] arrList){ int arrLength = getArrayLength(arrList); String retString=""; for(int i=0;i<arrLength;i++){ retString=retString+arrList[i]; } return retString; } public String reverse(String str){ char[] chrArray=str.toCharArray(); int arrLength=getArrayLength(chrArray); char tmp; for(int i=0,j=arrLength-1;i<arrLength/2;i++,j--){ tmp=chrArray[j]; chrArray[j]=chrArray[i]; chrArray[i]=tmp; } return convertArrString(chrArray); } } File2: MainClass.java ------------------------------------------------------------------ package org.developersbrain.solutions; public class MainClass { public static void main(String[] args){ Strin

Write method to replace all spaces in a string with '%20'.

File1: StringRoutines.java ----------------------------------------------------------------------------------- package org.developersbrain.solutions; public class StringRoutines { /*Find and replace a sequence of characters*/ int getArrayLength(char[] arrList){ return arrList.length; } char[] replace(char[] arrList,int startPos,int endPos,char[] findChar,char[] repChar){ int findLength=getArrayLength(findChar); int repLength=getArrayLength(repChar); char[] chrRep = null; if(findLength==repLength){ for(int i=startPos,k=0;i<endPos;i++,k++) arrList[i]=repChar[k]; }else if(repLength>findLength){ int diff=(repLength-findLength); int arrLength=getArrayLength(arrList); chrRep=new char[arrLength+diff]; int i,j; for(i=0,j=0;i<arrLength;j++,i++){ chrRep[j]=arrList[i]; if(j==startPos){ for(int k=0;k<repLength;k++,j++){ chrRep[j]=repChar[k]; } j--; i=(j-diff); } } }else{

Singly Linked List Generic Implementation in Java

File: Node.java package org.developersbrain.solutions; //Linked List implementation using Recursive methods //This is a generic implementation public class Node<T> { Node<T> nextNode; T t; Node(T t){ this.t = t; nextNode=null; } public Node<T> insertRear(Node<T> next){ if(next.nextNode!=null) { next=insertRear(next.nextNode); } return next; } public void insert(T t){ Node<T> newN=new Node<T>(t); Node<T> lastNode=insertRear(this); //Looped through all elements to find the last element //Because the insert is always at the end in the singly linked list lastNode.nextNode=newN; } private Node<T> printNode(Node<T> node){ if(node != null){ System.out.println(node.t.toString()); //You have to overwrite the generic original class for the method to_string //to print the output as the way you want node=printNode(node.nextNode); } return node; } pu

Are given two strings anagram? Here is the implementation in Java.

package org.Solutions; import java.util.Arrays; public class StringRoutines { void bubbleSort(char[] chrArray){ int arrLength= chrArray.length; int k=0; char tmp; for(int m=arrLength;m>0;m--){ for(int i=0;i<arrLength-1;i++){ k=i+1; if(chrArray[i] > chrArray[k]){ tmp = chrArray[k]; chrArray[k]=chrArray[i]; chrArray[i]=tmp; } } } } String sortStringCharacters(String str){ char[] chr=new char[str.length()]; chr=str.toCharArray(); bubbleSort(chr); str=""; for(int i=0;i<chr.length;i++) str=str+chr[i]; return str; } public boolean isAnagaram(String str1,String str2){ boolean anag_Result=false; if(str1 != null && str2 != null && str1.length()==str2.length()){ str1 = sortStringCharacters(str1); str2 = sortStringCharacters(str2); return str1.equals(str2); }else{ anag_Result = false; } return anag_Result; } } Main Class: ------------------- package org.Solutions; public class MainClass { public static void main(String[] args) { Str

Write Code to Remove Duplicates from a Singly Linked List.

File1: Employee.java ------------------------------------------------------- package org.developersbrain.solutions; public class Employee { int empID; String empFirstName; String empLastName; String dateOfBirth; String  empZipCode; public Employee(){ empID = 0; empFirstName = ""; empLastName  = ""; dateOfBirth = "<YYYYMMDD>"; empZipCode = "000-0000"; } public Employee(int empID,String empFirstName,String empLastName,String dateOfBirth,String empZipCode){ this.empID  = empID; this.empFirstName = empFirstName; this.empLastName  = empLastName; this.dateOfBirth = dateOfBirth; this.empZipCode = empZipCode; } public int getEmpID() { return empID; } public void setEmpID(int empID) { this.empID = empID; } public String getEmpFirstName() { return empFirstName; } public void setEmpFirstName(String empFirstName) { this.empFirstName = empFirstNam