Skip to content

Instantly share code, notes, and snippets.

@gabhi
gabhi / gist:27ab7f6d37435333cb16
Last active August 29, 2015 14:02
Edit distance
public static int minDistance(String word1, String word2) {
int len1 = word1.length();
int len2 = word2.length();
// len1+1, len2+1, because finally return dp[len1][len2]
int[][] dp = new int[len1 + 1][len2 + 1];
for (int i = 0; i <= len1; i++) {
dp[i][0] = i;
}
@gabhi
gabhi / gist:3e36878c7cf8a1c18d57
Last active August 29, 2015 14:02
Inversion count
import java.util.Arrays;
public class InversionCount {
public static void main(String[] args) {
int[] input = { 2, 4, 1, 3, 5 };
System.out.println("Inversion count :" + invCount(input));
}
private static int invCount(int[] arr) {
if (arr.length < 2)
@gabhi
gabhi / gist:0053143d04c7df527891
Last active August 29, 2015 14:02
Matrix Rotate in place
import java.util.Arrays;
public class MatrixRotation {
public static void main(String[] args) {
System.out.println("Matrix rotation");
int[][] a = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
print2DArray(a);
@gabhi
gabhi / gist:d217d177f6c43c03e4bd
Last active August 29, 2015 14:03
Majority element
public class MajorityElement {
public static void main(String[] args) {
System.out.println("Majority Element");
int a[] = { 1, 3, 3, 1, 3 };
printMajority(a);
}
private static void printMajority(int[] a) {
int size = a.length;
int element = findCandidate(a, size);
public class ITOA {
public static String convert(int x, int base) {
boolean negative = false;
String s = "";
if (x == 0)
return "0";
negative = (x < 0);
if (negative)
x = -1 * x;
while (x != 0) {
@gabhi
gabhi / gist:510e1c361363ae3d12cd
Created June 26, 2014 20:49
Kth Largest element in array - java
public class KthLargest {
public static int findKLargest(int[] ar, int low, int high, int k) {
int pivotIndex = partition(ar, low, high);
if (pivotIndex == k) {
return ar[pivotIndex];
}
else if (pivotIndex > k) {
@gabhi
gabhi / gist:00f49c93ba0a2c917546
Last active August 29, 2015 14:03
three sum java code
import java.util.Arrays;
public class tsum {
public static void main(String[] args) {
int A[] = { 1, 4, 45, 6, 10, 8 };
int sum = 19;
boolean t = threeSumTo(A, sum);
System.out.println(t);
}
@gabhi
gabhi / gist:16903a8d31c0f7a815f9
Created June 26, 2014 23:25
Array rotate in place
public class ArrayRotate {
static void leftRotate(int arr[], int d, int n) {
rvereseArray(arr, 0, d - 1);
rvereseArray(arr, d, n - 1);
rvereseArray(arr, 0, n - 1);
}
static void rvereseArray(int arr[], int start, int end) {
int i;
int temp;
@gabhi
gabhi / gist:e3ed4220c13f4b071abc
Created June 26, 2014 23:29
String permutations Java
public class StringPermutations {
public static void main(String[] args) {
permuteString("abc");
}
private static void permuteString(String str) {
permutation("", str);
}
@gabhi
gabhi / gist:776c48ca92ffd691577e
Created June 27, 2014 18:46
pTtern in matrix
package cccc;
public class PatternInMatrix {
static boolean used[][];
public static boolean findPattern(char[][] matrix, int nRow, int nCol, char[] pattern) {
used = new boolean[nRow][nCol];
for (int i = 0; i < nRow; i++)
for (int j = 0; j < nCol; j++) {