forked from ephremdeme/data-structure-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome_detection.java
More file actions
37 lines (25 loc) · 866 Bytes
/
palindrome_detection.java
File metadata and controls
37 lines (25 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.*;
import java.lang.*;
/**
* Palindrome
*/
public class Palindrome {
static boolean isPalindrome(String s) {
// Left and right pointers pointing to the beginning and the end of the string
int left = 0, right = s.length() - 1;
while (left < right) {
// If there is a mismatch
if (s.charAt(left) != s.charAt(right))
return false;
// Increment left pointer and decrement right pointer
left++;
right--;
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine(); //Input a string
System.out.println(isPalindrome(str)); // Print whether the input string is palindrome or not
}
}