Problem statement:
Given a string. Can you check whether it is a palindrome or not?
Given a string. Can you check whether it is a palindrome or not?
- public class Palindrome {
- public static void main(String[] args) {
- String s = "abcba";
- char c[] = s.toCharArray();
- int p = c.length - 1;
- for (int i = 0; i < c.length / 2; i++) {
- if (c[i] == c[p]) {
- p--;
- } else {
- System.out.println("Not palindrome");
- return;
- }
- }
- System.out.println("Palindrome");
- }
- }
Output:
Palindrome
Time complexity: O(n/2)
No comments:
Post a Comment