correct the syntax errors using System; class GFG { void KMPSearch(string pat, string txt) { int M = pat.Length; int N = txt.Length; int[] lps = new int[M]; int j = 0; int i = 0; while (i < N) { if (pat[j] == txt[i]) { j++; i++; } if (j == M) { Console.Write("Found pattern " j = lps[j - 1]; } else if (i < N && pat[j] != txt[i]) { if (j != 0) j = lps[j - 1]; else i = i + 1; } } } void computeLPSArray(string pat, int M, int[] lps) { int len = 0; int i = 1; lps[0] = 0; while (i < M) { if (pat[i] == pat[len]) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = len; i++; } } } } public static void Main() { string txt = "ABABDABACDABABCABAB"; new GFG().KMPSearch(pat, txt); } }
correct the syntax errors
using System;
class GFG {
void KMPSearch(string pat, string txt)
{
int M = pat.Length;
int N = txt.Length;
int[] lps = new int[M];
int j = 0;
int i = 0;
while (i < N) {
if (pat[j] == txt[i]) {
j++;
i++;
}
if (j == M) {
Console.Write("Found pattern "
j = lps[j - 1];
}
else if (i < N && pat[j] != txt[i]) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
}
void computeLPSArray(string pat, int M, int[] lps)
{
int len = 0;
int i = 1;
lps[0] = 0;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0) {
len = lps[len - 1];
}
else
{
lps[i] = len;
i++;
}
}
}
}
public static void Main()
{
string txt = "ABABDABACDABABCABAB";
new GFG().KMPSearch(pat, txt);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 4 images