Sto lavorando su C# per trovare tutte le sottostringhe comuni tra due stringhe. Per esempio, se l'ingresso è: S1 = "bisogno asssitance con la posta elettronica" S2 = "assistenza e-mail necessaria"Tutte le sottostringhe comuni tra due stringhe
L'output dovrebbe essere- 'bisogno di assistenza e-mail'
Il codice di seguito restituisce la più lunga comuni sottostringa, ma voglio che il mio codice restituisca tutte le sottostringhe comuni. Ogni aiuto è molto apprezzato!
static void commonsubstrings()
{
input1 = "need asssitance with email";
input2 = "email assistance needed"
if (input2.Length > input1.Length)
{
swap = input1;
input1 = input2;
input2 = swap;
}
int k = 1;
String temp;
String longTemp = "";
for (int i = 0; (i <= input1.Length); i++)
{
if ((i == input1.Length))
{
if (longest != null)
{
k = longest.Length + 1;
}
else
{
k = 1;
}
temp = input1.Substring(1, input1.Length - 1);
if (temp.Equals(""))
{
break;
}
if (k <= temp.Length)
{
i = k - 1;
input1 = temp;
if ((longest != null) && (longest.Length > longTemp.Length))
{
longTemp = longest;
}
}
}
holder1 = input1.Substring(0, k);
for (int j = 0; (j < input2.Length) && (j + k <= input2.Length); j++)
{
check1 = input2.Substring(j, k);
if (holder1.Equals(check1))
{
longest = holder1;
break;
}
}
k++;
}
Console.WriteLine(longest);
Console.ReadLine();
}
Il risultato deve essere in qualsiasi ordine? – nerdybeardo
Tutte le sottostringhe comuni? Personaggi singoli? "Ema" e "Emai" e "Email" sono tre diverse sottostringhe corrispondenti? – Amy
Intendevi avere troppi caratteri "s" in input1? Questa è la parte della domanda o un refuso? Stai cercando di dire che "asssistance" e "assistenza" sono comuni? –