2. Check whether a string is palindrome or not
2. Check whether a string is palindrome or not
Problem link :- click here
package programs;
public class String__2
{
/*
Given a string s, check if it is palindrome or not.
First copy the string to a char type.
then check if the array is palindrome.
*/
static Boolean palindromeCheck(String s)
{
String st = s.toLowerCase();
char str[] = st.toCharArray();
int i, j;
i = 0;
j = str.length - 1;
for(i=0, j=str.length-1; i<j; i++, j--)
{
if(str[i] != str[j])
return false;
}
return true;
}
public static void main(String[] args)
{
String s = "Malayalam";
System.out.println("Given string : " + s);
Boolean ans = palindromeCheck(s);
if(ans)
System.out.println("The given string is palindrome");
else
System.out.println("The given string is not palindrome");
}
}
Time complexity :- O(n)
Space complexity :- O(1)
Comments
Post a Comment