Saturday, August 24, 2019

Can you write an algorithm to check palindrome?

Problem statement:
Given a string. Can you check whether it is a palindrome or not?
  1. public class Palindrome {
  2. public static void main(String[] args) {
  3. String s = "abcba";
  4. char c[] = s.toCharArray();
  5. int p = c.length - 1;
  6. for (int i = 0; i < c.length / 2; i++) {
  7. if (c[i] == c[p]) {
  8. p--;
  9. } else {
  10. System.out.println("Not palindrome");
  11. return;
  12. }
  13. }
  14. System.out.println("Palindrome");
  15. }
  16. }
Output:
Palindrome
Time complexity: O(n/2)

No comments:

Post a Comment

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...