JSFiddle - React, Tailwind, and code Playground

by Salmin Skenderovic

JavaScript

// "ABAZDC", "BACDBAD" => "ABAD"
// A => 0, OK
// B => 1, OK
// A => OK
// 
//
//
//
// "AGGTAB", "GXTXAYB" => "GTAB"
// "aaaa", "aa" => "aa"


function longestCommon(s1, s2) {
  const allSubs = [];

  for (let i = 0; i < 1; i++) {
    allSubs.push(getSubSequance(i, s1, s2))
  }

  console.log(allSubs)
}


// Returns 1 subseq
function getSubSequance(i, s1, s2) {
  const sub = []

	let left = i;
  let right = i;

  while (left < s1.length) {
    const result = findIndex(right, s2, s1[i]);
    if (!result) {
      return sub;
    } else {
      sub.push(s1[i])
      right = result;
    }
  }

  return sub;
}

function findIndex(start, s2, matchingChar) {
	console.log("checking from", start, "matching with", matchingChar)
  for (let i = start; i < s2.length; i++) {
    if (s2[i] == matchingChar) {
    	console.log("found", i)
      return i;
    }
  }
  return false;
}



function test(s1, s2, r) {
  longestCommon(s1, s2);
}

test("ABAZDC", "BACDBAD", "ABAD")