-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome.java
More file actions
25 lines (21 loc) · 935 Bytes
/
Copy pathPalindrome.java
File metadata and controls
25 lines (21 loc) · 935 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
package Algorithms.TwoPointer;
//Check whether given string is Palindrome
// A string is a palindrome if it reads the same forward as backward. For example: "abcdcba", or "racecar".
public class Palindrome {
public static void main(String[] args) {
System.out.println(" The Given String ' abcdcba' is palindrome: "+checkPalindromeUsingTwoPointer("abcdcba"));
System.out.println(" The Given String ' hello' is palindrome: "+checkPalindromeUsingTwoPointer("hello"));
System.out.println(" The Given String ' racecar' is palindrome: "+checkPalindromeUsingTwoPointer("racecar"));
}
//Using two Pointer Approach
private static boolean checkPalindromeUsingTwoPointer(String input) {
int left = 0, right = input.length() - 1;
while (left < right) {
if (input.charAt(left) != input.charAt(right)) {
return false;
}
left++; right--;
}
return true;
}
}