-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPalindrome.java
More file actions
38 lines (30 loc) · 789 Bytes
/
Palindrome.java
File metadata and controls
38 lines (30 loc) · 789 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
38
// Java implementation of the approach
public class Solution {
// Function that returns true if
// str is a palindrome
static boolean isPalindrome(String str) {
// Pointers pointing to the beginning
// and the end of the string
int i = 0, j = str.length() - 1;
// While there are characters toc compare
while (i < j) {
// If there is a mismatch
if (str.charAt(i) != str.charAt(j))
return false;
// Increment first pointer and
// decrement the other
i++;
j--;
}
// Given string is a palindrome
return true;
}
// Driver code
public static void main(String[] args) {
String str = "geeks";
if (isPalindrome(str))
System.out.print("Yes");
else
System.out.print("No");
}
}