(sto scrivendo questo nel contesto di JavaScript, ma accetterà una risposta corretta algoritmicamente in qualsiasi lingua)Trova la più piccola stringa univoco per ogni stringa in un array
Come si fa a trovare la sottostringa più corta di ogni elemento in una matrice di stringhe in cui la sottostringa NON è contenuta in nessuno degli altri elementi, ignorando il caso?
Supponiamo che io sono una serie di input come ad esempio:
var names = ["Anne", "Anthony", "LouAnn", "Kant", "Louise", "ark"];
L'output dovrebbe essere qualcosa del tipo:
var uniqueNames = ["ne", "h", "ua", "ka", "i", "r"];
Per i miei scopi, si può tranquillamente supporre che nessun elemento sarà interamente dentro un altro elemento
I miei pensieri:
Sembra che uno potrebbe probabilmente forza bruta questo, lungo le linee di:
var names = ["Anne", "Anthony", "LouAnn", "Kant", "Louise", "ark"];
var uniqueNames = [], nameInd, windowSize, substrInd, substr, otherNameInd, foundMatch;
// For each name
for (nameInd = 0; nameInd < names.length; nameInd++)
{
var name = names[nameInd];
// For each possible substring length
windowLoop:
for (windowSize = 1; windowSize <= name.length; windowSize++)
{
// For each starting index of a substring
for (substrInd = 0; substrInd <= name.length-windowSize; substrInd++)
{
substr = name.substring(substrInd,substrInd+windowSize).toLowerCase();
foundMatch = false;
// For each other name
for (otherNameInd = 0; otherNameInd < names.length; otherNameInd++)
{
if (nameInd != otherNameInd && names[otherNameInd].toLowerCase().indexOf(substr) > -1)
{
foundMatch = true;
break;
}
}
if (!foundMatch)
{
// This substr works!
uniqueNames[nameInd] = substr;
break windowLoop;
}
}
}
}
Ma devo immaginare che ci sia una soluzione più elegante utilizzando tentativi/alberi prefisso, array suffisso, o qualcosa di interessante come quello.
Edit: Credo che questa è la forma della risposta selezionata avrebbe preso a livello di codice in JavaScript:
var names = ["Anne", "Anthony", "LouAnn", "Kant", "Louise", "ark"];
var uniqueNames = [], permutations = {}, permutation, nameInd, windowSize, substrInd, substr;
// For each name
for (nameInd = 0; nameInd < names.length; nameInd++)
{
var name = names[nameInd];
// For each possible substring length
windowLoop:
for (windowSize = 1; windowSize <= name.length; windowSize++)
{
// For each starting index of a substring
for (substrInd = 0; substrInd <= name.length-windowSize; substrInd++)
{
substr = name.substring(substrInd,substrInd+windowSize).toLowerCase();
permutations[substr] = (typeof permutations[substr] === "undefined")?nameInd:-1;
}
}
}
for (substr in permutations)
{
permutation = permutations[substr];
if (permutation !== -1 && ((typeof uniqueNames[permutation] === "string" && substr.length < uniqueNames[permutation].length) || typeof uniqueNames[permutation] === "undefined"))
{
uniqueNames[permutation] = substr;
}
}
L'output del campione è errato? Non vedo 's' e' y' in là mentre si vede 'i, h' e' r' ... – Icarus
@Icarus Ah, buon punto. 's' e' y' non sono presenti solo perché non sto cercando tutte le sottostringhe più piccole che si adattano ai criteri, ma ognuno è abbastanza buono. Accetterei una risposta che restituisse una serie bidimensionale di tutti loro, ma non ho davvero bisogno di quel livello di dettaglio. Un'uscita ugualmente valida potrebbe essere 'var uniqueNames = [" ne "," y "," ua "," ka "," i "," s "];' – Patrick
È possibile limitare l'alfabeto di input a 26 caratteri (o qualcosa del genere, limitatelo)? –