-
Notifications
You must be signed in to change notification settings - Fork 26
/
pallindrome-check.cpp
42 lines (27 loc) · 934 Bytes
/
pallindrome-check.cpp
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
// Approach 1- Concatation:
// new = ""
// new += string[n-1]
// n--
// return new==string
// Space - O(n)
// Time - O(n^2) , because new is also iterating itself in the loop.
// Approach 2- Array/Vector:
// new = []
// new.append(string[n-1])
// n--
// string s(new)
// return s==string
// new.join => converts array into string in python
// Space - O(n)
// Time - O(n)
// Approach 3- Recursion:
// return first==last && isPallindrome(mid);
// Space will be O(1)?, No that's not the case with recursion,because of call stacks it uses, => 0(n)
// It will be O(1), if it's a Tail Recursion(last line of the call), here there is no additional penalty.
// Time - O(n)
// BEST WAY
// Approach 4- Pointers
// We can maintain 2 pointers l and r, which points to first and last character of a string initally, then we can compare l & r, if it's equal then increment,
// otherwise return false;
// Space - O(1)
// Time - O(n)