admin管理员组

文章数量:1379618

I have array:

let arr = ["logerror", "log:today", "log:1"]

I am looking for function how to get longest substring from this items.

Result:

log

Another example:

   let arr = ["dog+ěě+", "dog15qwqqq", "dogggggg"]

Result:

dog

Sure, I can write some algorithm, but is there any simple way?

How? Thanks

I have array:

let arr = ["logerror", "log:today", "log:1"]

I am looking for function how to get longest substring from this items.

Result:

log

Another example:

   let arr = ["dog+ěě+", "dog15qwqqq", "dogggggg"]

Result:

dog

Sure, I can write some algorithm, but is there any simple way?

How? Thanks

Share Improve this question asked Dec 19, 2017 at 10:01 friageacfriageac 291 silver badge3 bronze badges 8
  • 1 What would be a substring in your context? – connexo Commented Dec 19, 2017 at 10:02
  • 1 See examples in question – friageac Commented Dec 19, 2017 at 10:03
  • Writing an algorithm looks like a good idea. What have you tried so far ? – klugjo Commented Dec 19, 2017 at 10:03
  • Excuse me, maybe you misunderstood me. I do not know what it is. That's what I'm looking for – friageac Commented Dec 19, 2017 at 10:04
  • @klugjo First I ask more experienced people if there is no function / prototype in javascript – friageac Commented Dec 19, 2017 at 10:05
 |  Show 3 more ments

5 Answers 5

Reset to default 4

If you can phrase your question succinctly, you can often find what to search for. In this case, it looks like:

"Find the longest mon substring from within an array of strings"

A quick google reveals an algorithm for finding the largest mon substring between two strings:

https://en.wikibooks/wiki/Algorithm_Implementation/Strings/Longest_mon_substring

I don't want to copy the code as written there, as unsure of the copyright, but you could take the implementation and take something that will work with your array.

I would note that for large arrays, this may turn out to be a lengthy operation...

I used a simple approach:

It sorts the array using sort() method.

Then, the most important step is to look just at the first and last items.

function monSubsequence(array){
    let sortedArray = array.sort(); 
    let first = sortedArray[0];
    let last = sortedArray.pop();
    let length = first.length;
    let index = 0;
    
    while(index<length && first[index] === last[index])
        index++;
    return first.substring(0, index);
}
console.log(monSubsequence(["logerror", "log:today", "log:1"]));
console.log(monSubsequence(["dog+ěě+", "dog15qwqqq", "dogggggg"]));

Here is my suggestion

function subStrArr(arr) {
  let chars = arr[0].split(""), sub = "";
  for (let i=0;i<chars.length;i++) {
    for (let j=1;j<arr.length;j++) {
      if (arr[j].indexOf(chars[i])==-1) return sub;
    }
    sub+=chars[i];
  }
}  

let arr1 = ["logerror", "log:today", "log:1"];
let arr2 = ["dog+ěě+", "dog15qwqqq", "dogggggg"];

console.log(subStrArr(arr1))
console.log(subStrArr(arr2))

After some looking around I went for the string-algorithms npm package, which did the job nicely for me.

From the docs:

import { longestCommonSubstring } from 'string-algorithms';

const strings = [
  '12apple',
  '3apple4',
  'apple56'
];

console.log(longestCommonSubstring(strings));

produces the output apple.

without DP approach

var lcs = function (n, m) {
    let lcs = 0 //to store longest mon substring
    let s1 = n.length
    let s2 = m.length

    for(let i = 0;i < s1;i++){
        for(let j = 0; j< s2;j++){
            let track = 0
            //if letter are same, do while to check next letter
            if(n[i] == m[j]){
                while(i + track < s1 && j + track < s2 && n[i + track] == m[j + track]){
                    track += 1 // to track
                    if (lcs < track) {
                        lcs += 1
                    }
                }
            }
        }
    }

    return lcs;
};

var m = "abcdxyz"
var n = "xyzabcd" // 4

// var m = "dadef"
// var n = "adwce"//2

// var m = "acdghr";
// var n = "bgh"; //2

// var m = "A"
// var n = "A" //1
console.log(lcs(m, n));

本文标签: filterHow to get longest substring from array of strings using javascriptStack Overflow