How to find the longest palindrome in a given string? [duplicate] - c#
This question already has answers here:
Write a function that returns the longest palindrome in a given string
(23 answers)
Closed 9 years ago.
Possible Duplicate:
Write a function that returns the longest palindrome in a given string
I know how to do this in O(n^2). But it seems like there exist a better solution.
I've found this, and there is a link to O(n) answer, but it's written in Haskell and not clear for me.
It would be great to get an answer in c# or similar.
I've found clear explanation of the solution here. Thanks to Justin for this link.
There you can find Python and Java implementations of the algorithm (C++ implementation contains errors).
And here is C# implementation that is just a translation of those algorithms.
public static int LongestPalindrome(string seq)
{
int Longest = 0;
List<int> l = new List<int>();
int i = 0;
int palLen = 0;
int s = 0;
int e = 0;
while (i<seq.Length)
{
if (i > palLen && seq[i-palLen-1] == seq[i])
{
palLen += 2;
i += 1;
continue;
}
l.Add(palLen);
Longest = Math.Max(Longest, palLen);
s = l.Count - 2;
e = s - palLen;
bool found = false;
for (int j = s; j > e; j--)
{
int d = j - e - 1;
if (l[j] == d)
{
palLen = d;
found = true;
break;
}
l.Add(Math.Min(d, l[j]));
}
if (!found)
{
palLen = 1;
i += 1;
}
}
l.Add(palLen);
Longest = Math.Max(Longest, palLen);
return Longest;
}
And this is its java version:
public static int LongestPalindrome(String seq) {
int Longest = 0;
List<Integer> l = new ArrayList<Integer>();
int i = 0;
int palLen = 0;
int s = 0;
int e = 0;
while (i < seq.length()) {
if (i > palLen && seq.charAt(i - palLen - 1) == seq.charAt(i)) {
palLen += 2;
i += 1;
continue;
}
l.add(palLen);
Longest = Math.max(Longest, palLen);
s = l.size() - 2;
e = s - palLen;
boolean found = false;
for (int j = s; j > e; j--) {
int d = j - e - 1;
if (l.get(j) == d) {
palLen = d;
found = true;
break;
}
l.add(Math.min(d, l.get(j)));
}
if (!found) {
palLen = 1;
i += 1;
}
}
l.add(palLen);
Longest = Math.max(Longest, palLen);
return Longest;
}
public static string GetMaxPalindromeString(string testingString)
{
int stringLength = testingString.Length;
int maxPalindromeStringLength = 0;
int maxPalindromeStringStartIndex = 0;
for (int i = 0; i < stringLength; i++)
{
int currentCharIndex = i;
for (int lastCharIndex = stringLength - 1; lastCharIndex > currentCharIndex; lastCharIndex--)
{
if (lastCharIndex - currentCharIndex + 1 < maxPalindromeStringLength)
{
break;
}
bool isPalindrome = true;
if (testingString[currentCharIndex] != testingString[lastCharIndex])
{
continue;
}
else
{
int matchedCharIndexFromEnd = lastCharIndex - 1;
for (int nextCharIndex = currentCharIndex + 1; nextCharIndex < matchedCharIndexFromEnd; nextCharIndex++)
{
if (testingString[nextCharIndex] != testingString[matchedCharIndexFromEnd])
{
isPalindrome = false;
break;
}
matchedCharIndexFromEnd--;
}
}
if (isPalindrome)
{
if (lastCharIndex + 1 - currentCharIndex > maxPalindromeStringLength)
{
maxPalindromeStringStartIndex = currentCharIndex;
maxPalindromeStringLength = lastCharIndex + 1 - currentCharIndex;
}
break;
}
}
}
if(maxPalindromeStringLength>0)
{
return testingString.Substring(maxPalindromeStringStartIndex, maxPalindromeStringLength);
}
return null;
}
C#
First I search for even length palindromes. Then I search for odd length palindromes. When it finds a palindrome, it determines the length and sets the max length accordingly. The average case complexity for this is linear.
protected static int LongestPalindrome(string str)
{
int i = 0;
int j = 1;
int oldJ = 1;
int intMax = 1;
int intCount = 0;
if (str.Length == 0) return 0;
if (str.Length == 1) return 1;
int[] intDistance = new int[2] {0,1};
for( int k = 0; k < intDistance.Length; k++ ){
j = 1 + intDistance[k];
oldJ = j;
intCount = 0;
i = 0;
while (j < str.Length)
{
if (str[i].Equals(str[j]))
{
oldJ = j;
intCount = 2 + intDistance[k];
i--;
j++;
while (i >= 0 && j < str.Length)
{
if (str[i].Equals(str[j]))
{
intCount += 2;
i--;
j++;
continue;
}
else
{
break;
}
}
intMax = getMax(intMax, intCount);
j = oldJ + 1;
i = j - 1 - intDistance[k];
}
else
{
i++;
j++;
}
}
}
return intMax;
}
protected static int getMax(int a, int b)
{
if (a > b) return a; return b;
}
Recently I wrote following code during interview...
public string FindMaxLengthPalindrome(string s)
{
string maxLengthPalindrome = "";
if (s == null) return s;
int len = s.Length;
for(int i = 0; i < len; i++)
{
for (int j = 0; j < len - i; j++)
{
bool found = true;
for (int k = j; k < (len - j) / 2; k++)
{
if (s[k] != s[len - (k - j + 1)])
{
found = false;
break;
}
}
if (found)
{
if (len - j > maxLengthPalindrome.Length)
maxLengthPalindrome = s.Substring(j, len - j);
}
if(maxLengthPalindrome.Length >= (len - (i + j)))
break;
}
if (maxLengthPalindrome.Length >= (len - i))
break;
}
return maxLengthPalindrome;
}
I got this question when i took an interview.
I found out when i was back home, unfortunately.
public static string GetMaxPalindromeString(string testingString)
{
int stringLength = testingString.Length;
int maxPalindromeStringLength = 0;
int maxPalindromeStringStartIndex = 0;
for (int i = 0; i < testingString.Length; i++)
{
int currentCharIndex = i;
for (int lastCharIndex = stringLength - 1; lastCharIndex > currentCharIndex; lastCharIndex--)
{
bool isPalindrome = true;
if (testingString[currentCharIndex] != testingString[lastCharIndex])
{
continue;
}
for (int nextCharIndex = currentCharIndex + 1; nextCharIndex < lastCharIndex / 2; nextCharIndex++)
{
if (testingString[nextCharIndex] != testingString[lastCharIndex - 1])
{
isPalindrome = false;
break;
}
}
if (isPalindrome)
{
if (lastCharIndex + 1 - currentCharIndex > maxPalindromeStringLength)
{
maxPalindromeStringStartIndex = currentCharIndex;
maxPalindromeStringLength = lastCharIndex + 1 - currentCharIndex;
}
}
break;
}
}
return testingString.Substring(maxPalindromeStringStartIndex, maxPalindromeStringLength);
}
Related
How do I fix my stack overflow error for Sudoku solver c#? Has to do with Recursive function?
I am trying to make a Sudoku solver. Whenever I try to run it it gives me a Stack Overflow Error: System.StackOverflowException: 'Exception of type 'System.StackOverflowException' was thrown.' I believe that it probably has to do with the solve function calling on the FindEmpy function too much? Any help is greatly appreciated! Thanks so much! using System; namespace sudokusolver { class Program { static public void PrintBoard(int[][] bo) { for (int i = 0; i < bo.Length; i++) { if (i % 3 == 0 && i != 0) { Console.WriteLine("- - - - - - - - - - - - -"); } for (int j = 0; j < bo[0].Length; j++) { if (j % 3 == 0 && j != 0) { Console.Write(" | "); } if (j == 8) { Console.WriteLine(bo[i][j]); } else { Console.Write(bo[i][j] + " "); } } } } static public (int,int) FindEmpty(int[][] bo) { for (int i = 0; i < bo.Length; i++) { for (int j = 0; j < bo[0].Length; j++) { if (bo[i][j] == 0) { return (i, j); } } } return (100, 100); } static public bool Solve(int[][] bo) { int x; int y; if (FindEmpty(bo) == (100, 100)) { return true; } else { y = FindEmpty(bo).Item1; x = FindEmpty(bo).Item2; } for (int i = 0; i < 10; i++) { if (IsValid(bo, i, x, y) == true) { bo[y][x] = i; if (Solve(bo) == true) { return true; } else { bo[y][x] = 0; } } } return false; } static public bool IsValid(int[][] bo, int num, int x, int y) { for (int i = 0; i < bo.Length; i++) { if (bo[y][i] == num && x != i) { return false; } } for (int i = 0; i < bo[0].Length; i++) { if (bo[i][x] == num && y != i) { return false; } } int boxx = x / 3; int boxy = y / 3; for (int i = boxy * 3; i < boxy * 3 + 3; i++) { for (int j = boxx * 3; j < boxx * 3 + 3; j++) { if (bo[i][j] == num && i != y && j != x) { return false; } } } return true; } static void Main(string[] args) { int[][] board = { new int[] {7,0,0,0,0,0,2,0,0}, new int[] {4,0,2,0,0,0,0,0,3}, new int[] {0,0,0,2,0,1,0,0,0}, new int[] {3,0,0,1,8,0,0,9,7}, new int[] {0,0,9,0,7,0,6,0,0}, new int[] {6,5,0,0,3,2,0,0,1}, new int[] {0,0,0,4,0,9,0,0,0}, new int[] {5,0,0,0,0,0,1,0,6}, new int[] {0,0,6,0,0,0,0,0,8} }; PrintBoard(board); Solve(board); PrintBoard(board); } } }
static public bool Solve(int[][] bo) { int x; int y; if (FindEmpty(bo) == (100, 100)) { return true; } else { y = FindEmpty(bo).Item1; x = FindEmpty(bo).Item2; } - for (int i = 0; i < 10; i++) + for (int i = 1; i < 10; i++) { if (IsValid(bo, i, x, y) == true) { bo[y][x] = i; if (Solve(bo) == true) { return true; } else { bo[y][x] = 0; } } } return false; } At some point IsValid(bo, 0, x, y) returns true, so you replace the zero with another zero forever.
Knuth Morris Pratt algorithm implementation
I'm trying to implement KMP algorithm. Part "if (W[i] == S[m + i])" returns index out of range exception and I can't get it to work. I was following example on Wikipedia: https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm static int[] KMPTable(string W) { int[] T = new int[W.Length]; int pos = 2; int cnd = 0; T[0] = -1; T[1] = 0; while (pos < W.Length) { if (W[pos - 1] == W[cnd]) { T[pos] = cnd + 1; cnd = cnd + 1; pos = pos + 1; } else if (cnd > 0) { cnd = T[cnd]; } else { T[pos] = 0; pos = pos + 1; } } return T; } static int[] KMPSearch(string S, string W) { int m = 0; int i = 0; int[] kmpNext = KMPTable(S); List<int> result = new List<int>(); while (m + i < S.Length) { if (W[i] == S[m + i]) { if (i == W.Length - 1) { result.Add(m); } i = i + 1; } else { m = m + i - kmpNext[i]; if (kmpNext[i] > -1) i = kmpNext[i]; else i = 0; } } return result.ToArray(); }
When m + i < S.Length, then it might be W[i] that is out of its index. Try checking with a step-by-step debug.
Counting Sort Implementation in C#
I am implementing counting sort But some thing is wrong with my code I am new in Programming Please help me to find an error. I am implenting it step by step . namespace ConsoleApplication1 { class Program { public static int[] a = { 0,0,0,5,4,8,9,9,7,3, 3, 2, 1 }; public static void Sorting() { int j = 0, i = 0, smallestvalue = 0, largestvalue = 0, n = a.Length, lengthof_B = 0, temp = 0, anothersmallestvalue; smallestvalue = largestvalue = a[0]; for (i = 0; i < n; i++) { if (smallestvalue > a[i]) { smallestvalue = a[i]; } else if (largestvalue < a[i]) { largestvalue = a[i]; } } int x = anothersmallestvalue = smallestvalue; lengthof_B = largestvalue - smallestvalue + 1; int[] b = new int[lengthof_B]; for (i = 0; i < lengthof_B && smallestvalue <= largestvalue; i++) { for (j = 0; j < n; j++) { if (smallestvalue == a[j]) { b[i] = b[i] + 1; } } b[i] = temp + b[i]; temp = b[i]; smallestvalue++; } int[] c = new int[a.Length]; // I think error here for (i = n - 1; i >= 0; i--) { anothersmallestvalue = x; for (j = 0; j <= lengthof_B ; j++) { if (a[i] == anothersmallestvalue) { temp = b[j]; c[temp - 1] = anothersmallestvalue; b[j] = b[j]; } anothersmallestvalue++; } } for (i = 0; i < c.Length; i++) { Console.WriteLine("c[i] : " + c[i]); } } } class Demo { static void Main(string[] args) { Program.Sorting(); Console.ReadLine(); } } } Desired Output is 000123457899 But output of my program is 000120457809
This Is Your Code Here I found a mistake. And your Code is too complex Please Go through your code Once more. for (i = n - 1; i >= 0; i--) { anothersmallestvalue = x; for (j = 0; j <= lengthof_B ; j++) { if (a[i] == anothersmallestvalue) { temp = b[j]; c[temp - 1] = anothersmallestvalue; b[j] = b[j] -1 ;// Possible Mistake I think here } anothersmallestvalue++; } } the very simple and stylish way is described and shown here. en.wikipedia.org/wiki/Counting_sort#The_algorithm
Normal sorting your two loops should look like this for (i = 0; i < lengthof_B - 1; i++) { for (j = i + 1; j < lengthof_B; j++) { } }
Search for an Array or List in a List
Have List<byte> lbyte Have byte[] searchBytes How can I search lbyte for not just a single byte but for the index of the searchBytes? E.G. Int32 index = lbyte.FirstIndexOf(searchBytes); Here is the brute force I came up with. Not the performance I am looking for. public static Int32 ListIndexOfArray(List<byte> lb, byte[] sbs) { if (sbs == null) return -1; if (sbs.Length == 0) return -1; if (sbs.Length > 8) return -1; if (sbs.Length == 1) return lb.FirstOrDefault(x => x == sbs[0]); Int32 sbsLen = sbs.Length; Int32 sbsCurMatch = 0; for (int i = 0; i < lb.Count; i++) { if (lb[i] == sbs[sbsCurMatch]) { sbsCurMatch++; if (sbsCurMatch == sbsLen) { //int index = lb.FindIndex(e => sbs.All(f => f.Equals(e))); // fails to find a match IndexOfArray = i - sbsLen + 1; return; } } else { sbsCurMatch = 0; } } return -1; }
Brute force is always an option. Although slow in comparison to some other methods, in practice it's usually not too bad. It's easy to implement and quite acceptable if lbyte isn't huge and doesn't have pathological data. It's the same concept as brute force string searching.
You may find Boyer-Moore algorithm useful here. Convert your list to an array and search. The algorithm code is taken from this post. static int SimpleBoyerMooreSearch(byte[] haystack, byte[] needle) { int[] lookup = new int[256]; for (int i = 0; i < lookup.Length; i++) { lookup[i] = needle.Length; } for (int i = 0; i < needle.Length; i++) { lookup[needle[i]] = needle.Length - i - 1; } int index = needle.Length - 1; var lastByte = needle.Last(); while (index < haystack.Length) { var checkByte = haystack[index]; if (haystack[index] == lastByte) { bool found = true; for (int j = needle.Length - 2; j >= 0; j--) { if (haystack[index - needle.Length + j + 1] != needle[j]) { found = false; break; } } if (found) return index - needle.Length + 1; else index++; } else { index += lookup[checkByte]; } } return -1; } You can then search like this. If lbyte will remain constant after a certain time, you can just convert it to an array once and pass that. //index is returned, or -1 if 'searchBytes' is not found int startIndex = SimpleBoyerMooreSearch(lbyte.ToArray(), searchBytes); Update based on comment. Here's the IList implementation which means that arrays and lists (and anything else that implements IList can be passed) static int SimpleBoyerMooreSearch(IList<byte> haystack, IList<byte> needle) { int[] lookup = new int[256]; for (int i = 0; i < lookup.Length; i++) { lookup[i] = needle.Count; } for (int i = 0; i < needle.Count; i++) { lookup[needle[i]] = needle.Count - i - 1; } int index = needle.Count - 1; var lastByte = needle[index]; while (index < haystack.Count) { var checkByte = haystack[index]; if (haystack[index] == lastByte) { bool found = true; for (int j = needle.Count - 2; j >= 0; j--) { if (haystack[index - needle.Count + j + 1] != needle[j]) { found = false; break; } } if (found) return index - needle.Count + 1; else index++; } else { index += lookup[checkByte]; } } return -1; } Since arrays and lists implement IList, there's no conversion necessary when calling it in your case. int startIndex = SimpleBoyerMooreSearch(lbyte, searchBytes);
Another way you could do with lambda expression int index = lbyte.FindIndex(e => searchBytes.All(i => i.Equals(e));
C# InsertionSort 1xforeach 2xfor
I want to create an insertionsort but I can't get any further. I solved it on my way until I get out of bounds, in the second "for-command" which I can't solve. I'm not sure if I am on the right way, but I want to keep the solution simple. int arrayzähler = 0; int[] Speicherarray = new int[randomarray.Length]; //ausgabearray[1] = randomarray[1]; //für vergleich foreach (int wert in randomarray) { if (wert == randomarray[0]) { Speicherarray[0] = wert; ausgabearray[0] = wert; arrayzähler++; continue; // erster wert = ausgabearray[0] } arrayzähler++; for (int i = arrayzähler - 1; i >= arrayzähler - 1; i--) { for (int a = arrayzähler - 2; a >= arrayzähler - 2; a--) { if (Speicherarray[i] < Speicherarray[a]) { Speicherarray[a] = Speicherarray[a + 1]; } else if (Speicherarray[i] >= Speicherarray[a]) { Speicherarray[a] = wert; ausgabearray[i] = Speicherarray[i]; } } } }
This is exact coding for insertion sort, public void Sort(int[] collection) { int inner, temp; for (int i = 1; i < collection.Length; i++) { temp = collection[i]; inner = i; while (inner > 0 && collection[inner - 1] >= temp) { collection[i] = collection[inner - 1]; --inner; } collection[inner] = temp; } Console.WriteLine("Printing Insertion Sorted Items"); Print(); }