Skip to content

Instantly share code, notes, and snippets.

@bittib
bittib / WordSearch.java
Last active December 9, 2016 07:02
Word Search
/*
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
[
["ABCE"],
/*
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
Time Complexity : O(N^2)
*/
@bittib
bittib / EditDistance.java
Created May 30, 2013 12:21
Edit Distance
/*
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
a) Insert a character
b) Delete a character
c) Replace a character
*/
public int editDistance(String w1, String w2){
@bittib
bittib / DecodeWays.java
Created June 2, 2013 15:18
Decode Ways
/*
A message containing letters from A-Z is being encoded to numbers using the following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Given an encoded message containing digits, determine the total number of ways to decode it.
For example,
@bittib
bittib / FlattenBinaryTreeToLinkedList.java
Last active July 20, 2017 15:18
Flatten Binary Tree to Linked List
/*
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1
/ \
2 5
/ \ \
@bittib
bittib / DivideTwoIntegers.java
Created June 8, 2013 09:44
Divide Two Integers
/*
* Divide two integers without using multiplication, division and mod operator.
*/
public int divide(int dividend, int divisor) {
if (divisor == 0) throw new IllegalArgumentException("divisor cannot be 0");
if (dividend == 0) return 0;
boolean neg = (dividend > 0 != divisor > 0);
long dend = dividend;
long dsor = divisor;
dend = Math.abs(dend);
@bittib
bittib / MorrisInOrderTraverse.java
Created June 10, 2013 04:14
Morris InOrder Traverse Algorithm
public static void morrisInOrderTraverse(TreeNode root){
TreeNode ptr = root;
while (ptr != null){
if (ptr.left == null){
visit(ptr);
ptr = ptr.right;
}else{
TreeNode node = ptr.left;
while (node.right != null && node.right != ptr)
node = node.right;
@bittib
bittib / WordLadderII.java
Last active December 9, 2016 06:54
WordLadder II
/*
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:
•Only one letter can be changed at a time
•Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
@bittib
bittib / Subsets.java
Created June 15, 2013 10:28
Subsets
/*
Given a set of distinct integers, S, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If S = [1,2,3], a solution is:
@bittib
bittib / WildCardMatch.java
Created June 21, 2013 08:28
Wildcard Match
/*
Implement wildcard pattern matching with support for '?' and '*'.
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).
The matching should cover the entire input string (not partial).
The function prototype should be:
bool isMatch(const char *s, const char *p)