rocks
BAN USERjavascript solution
function compress(x) {
let str =[];
let count =1;
let lastChar = x.charAt(0);
for(let i=1; i <x.length; i++){
if(x[i] === x[i-1]) {
count++;
} else {
str.push(`${lastChar}${count}`);
count = 1;
lastChar = x.charAt(i);
}
}
//add the last one
str.push(`${lastChar}${count}`);
return str.join('');
}
function decompress(x) {
let str= [];
for(let i=0; i <x.length - 1; i = i+2) {
str.push(append(x[i], x[i+1]));
}
return str.join('');
}
function append(char, time) {
let chars = "";
while(time > 0) {
chars += char;
time --;
}
return chars;
}
var compressed = compress("WWWWVVVVTRTYUUUUI");
console.log(compressed)
compressed = decompress(compressed);
console.log(compressed)
Javascript solution using Binary seach
let a = ["aaa", "aaa","bbb","cccc"]
let b = ["bbb","cccc","eeee"]
let c= ["eeee", "ffff","kkkk","kkkk"]
//output ["aaa", "aaa", "ffff", "kkkk", "kkkk"]
function bs(arr, s, e, k) {
while(s <= e) {
var mid = parseInt((s+e) / 2);
if(arr[mid] === k) {
return mid;
}
if(k > arr[mid]) {
s = mid + 1;
} else{
e = mid -1;
}
}
return -1;
}
// do a binry search on b, c array based on A, value
let map = [];
function merge() {
for(let i =0 ;i < a.length; i++) {
var left = bs(b, 0, b.length, a[i]);
var right = bs(c, 0, c.length, a[i]);
// if item is not found then add it array
if(left === -1 && right === -1) {
map.push(a[i]);
} else {
// remove from array of b
if(left !== -1) {
b.splice(left, 1);
}
// remove from array of c
if(right !== -1) {
c.splice(right, 1);
}
}
}
// do a binary serch on array c based on b's value.
for(let i =0 ;i < b.length; i++) {
var left = bs(c, 0, c.length, b[i]);
// if item is not found then add it array
if(left === -1) {
map.push(b[i]);
} else {
// remove from array of c
if(left !== -1) {
c.splice(left, 1);
}
}
}
// c has the left overs
for(let i =0 ;i < c.length; i++) {
map.push(c[i]);
}
// now sort
map.sort();
}
merge();
console.log(map)
Javascript solution
function perm(str1, str2, newArr, m, n, i) {
// Base case: If all characters of str1 and str2 have been
// included in output string, then print the output strin
if (m == 0 && n == 0) {
console.log(newArr)
return;// base condition
}
// If some characters of str1 are left to be included, then
// include the first character from the remaining characters
// and recur for rest
if (m !== 0) {
newArr[i] = str1.charAt(0);
perm(str1.substring(1), str2, newArr, m - 1, n, i + 1);
}
// If some characters of str2 are left to be included, then
// include the first character from the remaining characters
// and recur for rest
if (n !== 0) {
newArr[i] = str2.charAt(0);
perm(str1, str2.substring(1), newArr, m, n - 1, i + 1);
}
}
var str1 = "ab", str2 = "cd", i = 0;
perm(str1, str2, [], str1.length, str2.length, i);
Javascript solution
var arr = [1,2,2,2,2,3,4];
var m = 4;
var cur = 0, i, currElement = -1;
var count = 0;
console.log(arr)
for(var i = 0;i < arr.length;i++)
{
// set the current element
if (currElement != arr[i])
{
currElement = arr[i];
count = 0;
}
// the moment count is > than min, it wil stop incrementing the "curr" pointer
// and wait for next condition to match
// so that subsequent filling will resume and start removing the duplicates.
if (currElement == arr[i] && count < Math.min(m, 2))
{
count++;
// start filling the array with matching value
arr[cur] = arr[i];
cur++;
}
}
for(var i = cur;i < arr.length;i++) {
arr[i] = 0;
}
console.log(arr)
Javascript solution
var s1 = "string";
var s2 ="strong";
// o(m+n)
function common() {
var dict = {};
for(var i=0; i < s1.length; i++) {
dict[s1.charAt(i)] = 1;
}
var commonChars =[]; // this is optional if space is not an issue
for(var i=0; i < s2.length; i++) {
if( dict[s2.charAt(i)] == 1) {
commonChars.push(s2.charAt(i)); // this is optional we can simply print
}
}
dict = commonChars.join(""); //o(n)
console.log(dict);
}
common();
function binary(a,b,c,d){for(;b<=c;){var e=b+c>>1;if(a[e]===d)return e;d>=a[e]?b=e+1:c=e-1}return-1}var arr=[2,2,2,2,2,2,4,4,4,4,10,10,10,18,18,20,20,20,20,20],inp=4,i=binary(arr,0,arr.length-1,inp);if(console.log(i),-1!=i){for(var l=i-1,j=i+1,count=1;j<arr.length&&arr[i]===arr[j];)count++,j++;for(;l>=0&&arr[i]===arr[l];)count++,l--;console.log(count)
}
- rocks May 05, 2017Solution in java script
function binary(arr, s,e,v) {
while (s <= e) {
var m = (s+e) >> 1;
if(arr[m] === v) {
return m
} else if(v >= arr[m]) {
s = m+1;
} else {
e = m - 1;
}
}
return -1;
}
var arr = [2,2,2,2,2,2,4,4,4,4,10,10,10,18,18,20,20,20,20,20];
var inp = 4;
var i = binary(arr, 0, arr.length-1, inp);
console.log(i)
if(i != -1) {
var l = (i-1);
var j= (i+1);
var count=1;
while(j < arr.length && arr[i] === arr[j]) {
count++;
j++;
}
while(l >=0 && arr[i] === arr[l] ) {
count++;
l--;
}
console.log(count);
}
- rocks May 05, 2017var a = [1, 2, 4, -6, 5, 7, 9];
var b = [3, 6, 3, 4, 0];
// start the loop on smaller one
var n = 5;
var map = {};
for(var i=0; i < b.length;i++) {
map[(n - a[i])] = a[i];
}
for(var i=0; i < b.length;i++) {
if(map[b[i]]) {
console.log(b[i] +":"+map[b[i]])
}
}
//complexity o(m+n)
Javascript solutions.
- rocks May 12, 2017