Complete "longest common ..." examples (#100)
* no else return * fix var ref * fix importing/requiring dependencies * complete longest common examples
This commit is contained in:
committed by
Aditya Bhargava
parent
c23ca90b83
commit
5b675cc2e8
@@ -1,7 +1,54 @@
|
||||
if (word_a[i] === word_b[j]) {
|
||||
// The letters match
|
||||
cell[i][j] = cell[i - 1][j - 1] + 1;
|
||||
} else {
|
||||
// The letters don't match
|
||||
cell[i][j] = Math.max(cell[i - 1][j], cell[i][j - 1]);
|
||||
function createMatrix(rows, cols) {
|
||||
const matrix = new Array(rows);
|
||||
|
||||
for (let i = 0; i < matrix.length; i++) {
|
||||
matrix[i] = new Array(cols).fill(0);
|
||||
}
|
||||
|
||||
return matrix;
|
||||
}
|
||||
|
||||
function substring(a, b) {
|
||||
const cell = createMatrix(a.length + 1, b.length + 1);
|
||||
let lcs = 0;
|
||||
let lastSubIndex = 0;
|
||||
|
||||
for (let i = 1; i <= a.length; i++) {
|
||||
for (let j = 1; j <= b.length; j++) {
|
||||
if (a[i - 1] === b[j - 1]) {
|
||||
cell[i][j] = cell[i - 1][j - 1] + 1;
|
||||
|
||||
if (cell[i][j] > lcs) {
|
||||
lcs = cell[i][j];
|
||||
lastSubIndex = i;
|
||||
}
|
||||
} else {
|
||||
cell[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return a.slice(lastSubIndex - lcs, lastSubIndex);
|
||||
}
|
||||
|
||||
substring("vista", "hish"); // "is"
|
||||
substring("fish", "hish"); // "ish"
|
||||
|
||||
function subsequence(a, b) {
|
||||
const cell = createMatrix(a.length + 1, b.length + 1);
|
||||
|
||||
for (let i = 1; i <= a.length; i++) {
|
||||
for (let j = 1; j <= b.length; j++) {
|
||||
if (a[i] === b[j]) {
|
||||
cell[i][j] = cell[i - 1][j - 1] + 1;
|
||||
} else {
|
||||
cell[i][j] = Math.max(cell[i - 1][j], cell[i][j - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return cell[a.length][b.length];
|
||||
}
|
||||
|
||||
subsequence("fish", "fosh"); // 3
|
||||
subsequence("fort", "fosh"); // 2
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
const initializeMatrix = (rows, cols) => {
|
||||
export const initializeMatrix = (rows, cols) => {
|
||||
const matrix = [];
|
||||
for (let i = 0; i < rows.length; i += 1) {
|
||||
matrix.push(Array(cols.length).fill(0));
|
||||
}
|
||||
return matrix;
|
||||
};
|
||||
|
||||
export default initializeMatrix;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import base from './base';
|
||||
import { initializeMatrix } from "./base";
|
||||
|
||||
const diff = (firstWord, secondWord) => {
|
||||
const arr1 = firstWord.split('');
|
||||
const arr2 = secondWord.split('');
|
||||
const arr1 = firstWord.split("");
|
||||
const arr2 = secondWord.split("");
|
||||
const matrix = initializeMatrix(arr1, arr2);
|
||||
for (let i = 0; i < arr1.length; i += 1) {
|
||||
for (let j = 0; j < arr2.length; j += 1) {
|
||||
|
||||
@@ -1,11 +1,53 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
if word_a[i] == word_b[j] {
|
||||
cell[i][j] = cell[i-1][j-1] + 1
|
||||
} else {
|
||||
cell[i][j] = max(cell[i-1][j], cell[i][j-1])
|
||||
func createMatrix(rows, cols int) [][]int {
|
||||
cell := make([][]int, rows)
|
||||
for i := range cell {
|
||||
cell[i] = make([]int, cols)
|
||||
}
|
||||
|
||||
return cell
|
||||
}
|
||||
|
||||
func substring(a, b string) string {
|
||||
lcs := 0
|
||||
lastSubIndex := 0
|
||||
cell := createMatrix(len(a)+1, len(b)+1)
|
||||
|
||||
for i := 1; i <= len(a); i++ {
|
||||
for j := 1; j <= len(b); j++ {
|
||||
if a[i-1] == b[j-1] {
|
||||
cell[i][j] = cell[i-1][j-1] + 1
|
||||
|
||||
if cell[i][j] > lcs {
|
||||
lcs = cell[i][j]
|
||||
lastSubIndex = i
|
||||
}
|
||||
} else {
|
||||
cell[i][j] = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return a[lastSubIndex-lcs : lastSubIndex]
|
||||
}
|
||||
|
||||
func subsequence(a, b string) int {
|
||||
cell := createMatrix(len(a)+1, len(b)+1)
|
||||
|
||||
for i := 1; i <= len(a); i++ {
|
||||
for j := 1; j <= len(b); j++ {
|
||||
if a[i-1] == b[j-1] {
|
||||
cell[i][j] = cell[i-1][j-1] + 1
|
||||
} else {
|
||||
cell[i][j] = cell[i-1][j]
|
||||
|
||||
if cell[i][j] < cell[i][j-1] {
|
||||
cell[i][j] = cell[i][j-1]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return cell[len(a)][len(b)]
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSubstring(t *testing.T) {
|
||||
var stests = []struct {
|
||||
name, a, b, expected string
|
||||
}{
|
||||
{"hish-vista", "vista", "hish", "is"},
|
||||
{"hish-fish", "fish", "hish", "ish"},
|
||||
}
|
||||
|
||||
for _, tt := range stests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
actual := substring(tt.a, tt.b)
|
||||
|
||||
if actual != tt.expected {
|
||||
t.Errorf("Expected %s but received %s", tt.expected, actual)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubsequence(t *testing.T) {
|
||||
var stests = []struct {
|
||||
name, a, b string
|
||||
expected int
|
||||
}{
|
||||
{"fosh-fish", "fish", "fosh", 3},
|
||||
{"fosh-fort", "fort", "fosh", 2},
|
||||
}
|
||||
|
||||
for _, tt := range stests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
actual := subsequence(tt.a, tt.b)
|
||||
|
||||
if actual != tt.expected {
|
||||
t.Errorf("Expected %d but received %d", tt.expected, actual)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
export default function initialize_matrix(rows, cols){
|
||||
let matrix = [];
|
||||
for (let i = 0; i < rows.length; i++){
|
||||
matrix.push(Array(cols.length).fill(0));
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
module.exports = function initialize_matrix(rows, cols) {
|
||||
let matrix = [];
|
||||
for (let i = 0; i < rows.length; i++) {
|
||||
matrix.push(Array(cols.length).fill(0));
|
||||
}
|
||||
return matrix;
|
||||
};
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
import initialize_matrix as base from "./base.js";
|
||||
const initialize_matrix = require("./base.js");
|
||||
|
||||
export default function diff( firstWord, secondWord ){
|
||||
let arr1 = firstWord.split('');
|
||||
let arr2 = secondWord.split('');
|
||||
let matrix = initialize_matrix(arr1, arr2);
|
||||
for (let i = 0; i < arr1.length; i++){
|
||||
for (let j = 0; j < arr2.length; j++){
|
||||
if( arr1[i] == arr2[j] ){
|
||||
if( i > 0 && j > 0){
|
||||
matrix[i][j] = matrix[i - 1][j - 1] + 1;
|
||||
}else{
|
||||
matrix[i][j] = 1;
|
||||
}
|
||||
} else {
|
||||
if( i > 0 && j > 0){
|
||||
matrix[i][j] = Math.max(matrix[i - 1][j], matrix[i][j - 1]);
|
||||
}else{
|
||||
matrix[i][j] = 0;
|
||||
}
|
||||
}
|
||||
function diff(firstWord, secondWord) {
|
||||
let arr1 = firstWord.split("");
|
||||
let arr2 = secondWord.split("");
|
||||
let matrix = initialize_matrix(arr1, arr2);
|
||||
for (let i = 0; i < arr1.length; i++) {
|
||||
for (let j = 0; j < arr2.length; j++) {
|
||||
if (arr1[i] == arr2[j]) {
|
||||
if (i > 0 && j > 0) {
|
||||
matrix[i][j] = matrix[i - 1][j - 1] + 1;
|
||||
} else {
|
||||
matrix[i][j] = 1;
|
||||
}
|
||||
} else {
|
||||
if (i > 0 && j > 0) {
|
||||
matrix[i][j] = Math.max(matrix[i - 1][j], matrix[i][j - 1]);
|
||||
} else {
|
||||
matrix[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return matrix[arr1.length-1][arr2.length-1];
|
||||
}
|
||||
}
|
||||
return matrix[arr1.length - 1][arr2.length - 1];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user