Skip to content

Instantly share code, notes, and snippets.

@aniruddha84
aniruddha84 / letter_combinations.rb
Last active August 29, 2015 14:27
Letter combinations Problem
# Problem: https://leetcode.com/problems/letter-combinations-of-a-phone-number/
# Given a digit string, return all possible letter combinations that the number could represent.
# Input:Digit string "23"
# Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
@look_up = {
"2" => "abc",
"3" => "def",
"4" => "ghi",
@aniruddha84
aniruddha84 / parse_int.rb
Last active September 3, 2015 18:36
Parse string to Int
def number_map
{
"0" => 0,
"1" => 1,
"2" => 2,
"3" => 3,
"4" => 4,
"5" => 5,
"6" => 6,
"7" => 7,
@aniruddha84
aniruddha84 / sort_stack.rb
Created March 9, 2016 22:04
Sort a stack of integers
def stack_sort(stack)
result = []
until stack.empty?
t = stack.pop
while !result.empty? && result.last > t
stack.push result.pop
end
result.push t
end
@aniruddha84
aniruddha84 / find_next_successor.rb
Last active March 10, 2016 00:11
Find in-order successor of a node in a binary search tree. Each node has access to parent node.
def next_successor(bnode)
if bnode.right
curr = bnode.right
until curr.left.nil?
curr = curr.left
end
curr
else
curr = bnode
prev = bnode.parent
@aniruddha84
aniruddha84 / Find_lca_btree.rb
Created March 10, 2016 00:31
Find lowest common ancestor of 2 nodes in a Binary Tree
def find_lca(val1, val2, root)
stack = []
stack.push [root, []]
ancestor_list_val1 = []
ancestor_list_val2 = []
until stack.empty?
t = stack.pop
if t[0].data == val1
ancestor_list_val1 = t[1]
elsif t[0].data == val2
@aniruddha84
aniruddha84 / find_path.rb
Last active March 13, 2016 03:07
Given a start string, end string and a set of strings, find if there exists a path between the start string and end string via the set of strings.
# Given a start string, end string and a set of strings, find if there exists a path
# between the start string and end string via the set of strings.
# start: "cog" end: "bad"
# set: ["bag", "cag", "cat", "fag", "con", "rat", "sat", "fog"]
# one of the paths: "cog" -> "fog" -> "fag" -> "bag" -> "bad"
def path_exists?(start_str, end_str, arr)
return true if string_diff(start_str, end_str) == 1
arr.each do |s|
if string_diff(s, start_str) == 1
arr.delete(s)
@aniruddha84
aniruddha84 / sub_set_sum.rb
Last active April 6, 2016 22:55
Given an input array and target sum, find all possible ways the elements of the array can be added upto get target
# Given an input array and target sum, find all possible ways the elements of the array can be added upto get target
# sub_set_sum([10, 7, 5, 18, 12, 20, 15], 35) => [
# [10, 7, 18],
# [10, 5, 20]
# [5, 18, 12],
# [20, 15]
# ]
require 'set'
def subset_sum(arr, sum, curr_arr = [], result = Set.new)
return if arr.nil?
@aniruddha84
aniruddha84 / longest_nonrepeating_substring.rb
Last active May 7, 2016 18:07
Longest non-repeating substring
def longest_nonrepeating_substring(str)
max_len = 0
look_up = {}
len = 0
last_repeating_index = 0 # indicates beginning of unique string
(0...str.size).each do |index|
if (p = look_up[str[index]]) # repeat char found at index
if p > last_repeating_index
len = index - p
last_repeating_index = p
@aniruddha84
aniruddha84 / game_of_life.rb
Created May 7, 2016 18:26
Conway's Game of life
# @param {Integer[][]} board
# @return {Void} Do not return anything, modify board in-place instead.
def game_of_life(board)
m = board.size
n = board[0].size
meta_data = {}
(0...m).each do |row|
(0...n).each do |col|
living_neighbours = live_neighbours(board, row, col)
# meta-data format -> [row:col] => [old_value, num_of_live_neighbours]
@aniruddha84
aniruddha84 / remove-dups-sorted-arr.java
Last active January 15, 2018 22:34
Remove duplicates from Sorted Array
// https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
public void removeDuplicates(int[] arr) {
if (arr.length <= 1) return arr.length;
int p1 = 1;
for(int i = 1; i < arr.length; i++) {
if (arr[i] != arr[i-1]) {
arr[p1] = arr[i];
p1 += 1;
}
}