xxxxxxxxxx
Copy
public class Solution {
public bool IsPalindrome(int x) {
string first = x.ToString(); //turn to string (easy to reverse)
char[] charArr = first.ToCharArray(); //the original target
char[] reverseArr = first.ToCharArray();
Array.Reverse(reverseArr );
return charArr.SequenceEqual(reverseArr); //compare two array
}
}
xxxxxxxxxx
static bool IsPalindrome(int n)
{
return n.ToString() == Reverse(n.ToString());
}
static string Reverse(string str)
{
return new string(str.ToCharArray().Reverse().ToArray());
}
xxxxxxxxxx
Copy
public class Solution {
public bool IsPalindrome(int x) {
string k = x.ToString();
for(int i=0;i<k.Length/2;i++)
{
if(k[i] != k[k.Length-1-i])
{
return false; // if any char not the same, return false
}
}
return true;
}
}
xxxxxxxxxx
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a string: ");
string input = Console.ReadLine();
// Remove spaces and punctuation from the input string
string processedInput = new string(input.Where(char.IsLetterOrDigit).ToArray());
// Convert the input string to lowercase for case-insensitive comparison
string lowercaseInput = processedInput.ToLower();
// Reverse the input string
char[] charArray = lowercaseInput.ToCharArray();
Array.Reverse(charArray);
string reversedInput = new string(charArray);
// Check if the input string is a palindrome
bool isPalindrome = lowercaseInput == reversedInput;
Console.WriteLine(isPalindrome ? "Palindrome" : "Not a palindrome");
}
}
xxxxxxxxxx
using System;
public class PalindromeExample
{
public static void Main(string[] args)
{
int n,r,sum=0,temp;
Console.Write("Enter the Number: ");
n = int.Parse(Console.ReadLine());
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
Console.Write("Number is Palindrome.");
else
Console.Write("Number is not Palindrome");
}
}
xxxxxxxxxx
public class Solution {
public IList<IList<string>> Partition(string s) {
var result = new List<IList<string>>();
Backtrack(s, 0, new List<string>(), result);
return result;
}
// to understand below funcation check explanation part named 'How Backtrack Works '
private void Backtrack(string s, int start, List<string> path, List<IList<string>> result)
{
if (start == s.Length) {
result.Add(new List<string>(path));
return;
}
for (int end = start + 1; end <= s.Length; end++) {
if (IsPalindrome(s, start, end - 1)) {
path.Add(s.Substring(start, end - start));
Backtrack(s, end, path, result);
path.RemoveAt(path.Count - 1);
}
}
}
// to understand below funcation check explanation part named 'Find Palindrome String in C#'
private bool IsPalindrome(string s, int left, int right) {
while (left < right) {
if (s[left] != s[right]) {
return false;
}
left++;
right--;
}
return true;
}
}