Last active
September 27, 2016 18:50
-
-
Save RameshRM/3fb30eb1c5abe492713879e71ef95978 to your computer and use it in GitHub Desktop.
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 string, and an alphabet, find the shortest substring in the input containing every letter in the alphabet at least once | |
// “aaccbc”, {‘a’, ‘b’, ‘c’} => “accb" | |
// "aaaabbbcccdddcdba", [ 'a', 'b', 'c' ] => cdba | |
for (var i = 0; i < input.length; i++) { | |
for (var j = i; j < input.length; j++) { | |
var s = input.substring(i,j); | |
for (var c : s) { | |
if (matcher.contains(c)) { | |
} | |
} | |
} | |
} | |
function findShortest(input, matcher){ | |
if(input){ | |
var result = {}; | |
for(var i=0; i<input.length; i++){ | |
var matchingSub = input.substring(i,1); | |
for (var j=0; j<matcher.length; j++){ | |
} | |
} | |
} | |
} | |
// Given an array of integers, find the length of the longest arithmetic subsequence | |
// arithmetic sequence [0,1,2,...] or [1, -1, -3, -5...] or [1, 1, 1, 1, 1...] | |
// [1, 3, 8, 5, 10, 10, 10, 7] => 4 | |
// | |
// (1, 3 ,5, 7) => 4, (10, 10, 10) => 3 | |
{1 => 3 (2), 8(7), 5(4), 10 (9), 10(9), 7} | |
{3 => 8 (5) , | |
function longestSubseq(numbers){ | |
var subSequenceResult= {}; | |
for(var i=0; i< numbers.length; i++{ | |
for (var j=i; j<numbers.length; j++){ | |
var diff = numbers[j] > numbers[i] ? numbers[j] - numbers[i] : numbers[i] - numbers[j]; | |
if(subSequenceResult[diff]){ | |
subSequenceResult[diff].push(numbers[i]); // {2: [1 | |
subSequenceResult[diff].push(numbers[j]); // {2: [1,3 | |
}else{ | |
subSequenceResult[diff] = [numbers[i], numbers[j]]; | |
} | |
} | |
} | |
var longest = 0; | |
Object.keys(subSequenceResult).forEach(function forEach(key){ | |
longest = Math.max(longest,(subSequenceResult[key] || [] ).length); | |
}); | |
return longest; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment