-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindromeCheck.java
59 lines (45 loc) · 1.64 KB
/
PalindromeCheck.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class PalindromeCheck {
// BruteForce Approach
public static boolean isPalindrome(String str) {
String rev = "";
for (int i = str.length() - 1; i >= 0; i--) {
rev = rev + str.charAt(i);
}
return rev.toLowerCase().equals(str);
// The time complexity of concatenating strings in this manner is O(n2) because
// for each character appended, the entire current value of rev is copied.
// T.C=0(n2) S.C=O(n)
}
// Two Pointer Technique
public static boolean isPalindrome1(String str) {
for (int i = 0; i < str.length() - 1; i++) {
if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
return false;
}
}
return true;
// T.C=O(n) S.C=O(1)
}
// Recursion
public static boolean isPalindrome2(String str, int start, int end) {
if (start > end) {
return true;
}
if (str.charAt(start) == str.charAt(end)) {
return isPalindrome2(str, start + 1, end - 1);
}
return false;
// Time Complexity: O(n)
// Space Complexity: O(n) due to the call stack
}
public static void main(String[] args) {
System.out.println(isPalindrome("pratham"));
System.out.println(isPalindrome("roor"));
System.out.println(isPalindrome1("pratham"));
System.out.println(isPalindrome1("roor"));
String str = "pratham";
String str1 = "opopo";
System.out.println(isPalindrome2(str, 0, str.length() - 1));
System.out.println(isPalindrome2(str1, 0, str1.length() - 1));
}
}