-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-palindromic-subsequences.go
More file actions
57 lines (48 loc) · 967 Bytes
/
Copy pathremove-palindromic-subsequences.go
File metadata and controls
57 lines (48 loc) · 967 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import "fmt"
// source: https://leetcode.com/problems/remove-palindromic-subsequences/
func reverseString(str string) string {
s := []byte(str)
l := len(s)
for i := 0; i < l/2; i++ {
s[i], s[l-i-1] = s[l-i-1], s[i]
}
return string(s)
}
func removePalindromeSub_(s string) int {
if len(s) == 0 {
return 0
}
if reverseString(s) == s {
return 1
}
return 2
}
func isPalindrome(x string) bool {
for i, _ := range x[:len(x)/2] {
if x[i] != x[len(x)-1-i] {
return false
}
}
return true
}
func removePalindromeSub(s string) int {
if len(s) == 0 {
return 0
}
if isPalindrome(s) {
return 1
}
return 2
}
func main() {
// Example 1
var s1 string = "ababa"
fmt.Println("Expected: 1 Output: ", removePalindromeSub(s1))
// Example 2
var s2 string = "abb"
fmt.Println("Expected: 2 Output: ", removePalindromeSub(s2))
// Example 3
var s3 string = "baabb"
fmt.Println("Expected: 2 Output: ", removePalindromeSub(s3))
}