Problem statement:
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.
Given a string , print Yes if it is a palindrome, print No otherwise
Constraints:
A will consist at most 50 lower case english letters.
Sample Input:
madam
Sample Output:
Yes
Yes
Method-II:
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.
Given a string , print Yes if it is a palindrome, print No otherwise
Constraints:
A will consist at most 50 lower case english letters.
Sample Input:
madam
Sample Output:
Yes
- public class Palindrome {
- public static void main(String[] args) {
- String s = "madam";
- System.out.println(isPalindrome(s) ? "Yes" : "No");
- }
- public static boolean isPalindrome(String s) {
- int p = s.length() - 1;
- boolean flag = true;
- for (int i = 0; i < s.length() / 2; i++) {
- if (s.charAt(i) != s.charAt(p)) {
- flag = false;
- break;
- }
- p--;
- }
- return flag;
- }
- }
Yes
Method-II:
- public class Palindrome {
- public static void main(String[] args) {
- String s = "madam";
- isPalindrome(s);
- }
- public static void isPalindrome(String s) {
- String rev = "";
- for (int i = s.length() - 1; i >= 0; i--) {
- rev = rev + s.charAt(i);
- }
- if (s.equals(rev))
- System.out.println("Yes");
- elset a Palindrome");
- }
- }
Output:
Yes
No comments:
Post a Comment