Palindrome String InterviewBit Solution
Problem:
"ad A Man, A plan, a canal: Panama ad" is a palindrome.
Idea is to get an alphanumeric string with all lower case and check for palindrome.
Code:
public class Solution {
public int isPalindrome(String a) {
String temp=new String( a.replaceAll("[^A-Za-z0-9]", "").toLowerCase());
// System.out.println(temp);
char arr[]=temp.toCharArray();
int i=0,size=arr.length-1;
while(i<size)
{
if(arr[i]!=arr[size])
return 0;
i++;
size--;
}
return 1;
}
}
"ad A Man, A plan, a canal: Panama ad" is a palindrome.
Idea is to get an alphanumeric string with all lower case and check for palindrome.
Code:
public class Solution {
public int isPalindrome(String a) {
String temp=new String( a.replaceAll("[^A-Za-z0-9]", "").toLowerCase());
// System.out.println(temp);
char arr[]=temp.toCharArray();
int i=0,size=arr.length-1;
while(i<size)
{
if(arr[i]!=arr[size])
return 0;
i++;
size--;
}
return 1;
}
}
0 Comments:
Post a Comment