-
Notifications
You must be signed in to change notification settings - Fork 7
/
Palindrome Check.py
69 lines (51 loc) · 1.61 KB
/
Palindrome Check.py
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
60
61
62
63
64
65
66
67
68
69
"""
Palindrome Check
Write a function that takes in a non-empty string and tha returns a boolean representing
whether the string is a palindrome.
A palindrome is defined as a string that's written the same forward and backward.Note
that single-character strings are palindromes.
Sample Input
string = 'abcdcba'
Sample Output
true // it's written the same forward and backward
"""
# SOLUTION 1
# Running time O(n) | space 0(1)
def isPalindrome(string):
left = 0
right = len(string) - 1
while left < right:
if string[left] != string[right]:
return False
left += 1
right -= 1
return True
# SOLUTION 2
# Running time O(n^2) | space 0(1)
def isPalindrome(string):
reversedstring = ""
for i in reversed(range(len(string))):
reversedstring += string[i]
return string == reversedstring
# SOLUTION 3
# Running time O(n) | space 0(n)
def isPalindrome(string):
reversedchars = []
for i in reversed(range(len(string))):
reversedchars.append(string[i])
return string == "".join(reversedchars)
# SOLUTION 4
# Running time O(n) | space 0(n)
def isPalindrome(string, i=0):
j = len(string) - 1 - i
return True if i >= j else string[i] == string[j] and isPalindrome(string, i+1)
# SOLUTION 5
# Below we are using Tail Recursion
# Running time O(n) | space 0(1)
def isPalindrome(string, i=0):
j = len(string) - 1 - i
if i >= j:
return True
if string[i] != string[j]:
return False
return isPalindrome(string, i+1)