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
# 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", |
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
def number_map | |
{ | |
"0" => 0, | |
"1" => 1, | |
"2" => 2, | |
"3" => 3, | |
"4" => 4, | |
"5" => 5, | |
"6" => 6, | |
"7" => 7, |
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
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 |
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
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 |
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
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 |
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
# 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) |
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
# 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? |
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
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 |
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
# @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] |
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
// 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; | |
} | |
} |
OlderNewer