This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String reverse( String s ){ | |
char[] c = s.toCharArray(); | |
int size = c.length; | |
for( int i = 0; i<size/2; i ++ ){ | |
char temp = c[i]; | |
c[i] = c[size - i - 1]; | |
c[size - i - 1] = temp;; | |
} | |
return new String( c ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int recursiveFibonacci( int n ){ | |
if ( n<0 ){ | |
return -1; | |
} | |
if( n == 0 || n == 1 ){ | |
return 1; | |
} | |
return recursiveFibonacci( n -1 ) + recursiveFibonnaci( n - 2 ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Method 1 : Time : O(n^3), Space : O(1) | |
public boolean findLongestPalindrome( String word ){ | |
String longestWord = ""; | |
for( int i = 0; i < word.length(); i++ ){ | |
for( int j = 0; j<word.length(); j++ ){ | |
if( i<= j ){ | |
String currentWord = word.substring( i, j ); | |
if( isPalindrome( currentWord ) && currentWord.length() > longestWord.length() ){ | |
longestWord = currentWord; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Vertex{ | |
private int id; | |
private List<Edge> adjacencies; | |
private boolean isVisited; | |
public Vertex( int id ){ | |
this.id = id; | |
this.adjacencies = new ArrayList<Edge>(); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GraphBFS{ | |
public void doBFS( Vertex source ){ | |
Queue<Vertex> verticesToVisit = new LinkedList<Vertex>(); | |
verticesToVisit.enqueue( source ); | |
source.setVisited( true ); | |
while( !verticesToVisit.isEmpty() ){ | |
Vertex currentVertex = verticesToVisit.dequeue(); | |
for( Edge edge : currentVertex.getAdjacencies() ) { | |
Vertex destinationVertex = edge.getDestinationVertex(); | |
if( !destinationVertex.isVisited() ) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PrimsAlgorithm{ | |
public void findPrimsMST( Graph graph ){ | |
//Step 1 : Initialize necessary arrays | |
int[] weights = new int[graph.getNumOfVertices()]; | |
boolean[] visited = new boolean[graph.getNumOfVertices()]; | |
int[] predecessors = new int[graph.getNumOfVertices()]; | |
for( int i = 0; i<weights.length; i++ ){ | |
weights[i] = Integer.MAX_VALUE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void findRange( Node node, int k1, int k2 ){ | |
if( node == null || (k1>k2) ){ | |
return; | |
} | |
int data = node.getData(); | |
if( data >= k1 && data <= k2 ){ | |
System.out.println(data); | |
} | |
if( k1 < data ){ | |
findRange( node.getLeft(), k1, k2 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void doLevelOrder( Node root ){ | |
Queue<Node> nodes = new Queue<Node>(); | |
nodes.queue( root ); | |
while( !queue.isEmpty() ){ | |
Node currentNode = queue.dequeue(); | |
print( currentNode ); | |
if( currentNode.getLeft != null ){ | |
nodes.enqueue( currentNode.getLeft() ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void doSpiralOrder( Node root ){ | |
Queue<Node> queue = new Queue<Node>(); | |
Stack<Node> stack = new Stack<Node>(); | |
stack.push( root ); | |
boolean readStack = true; | |
while( (readStack && !stack.isEmpty()) || (!readStack && !queue.isEmpty()) ){ | |
Node currentNode = null; | |
if( readStack ){ | |
currentNode = stack.pop(); |