aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-145/pokgopun/go/ch-2.go66
1 files changed, 35 insertions, 31 deletions
diff --git a/challenge-145/pokgopun/go/ch-2.go b/challenge-145/pokgopun/go/ch-2.go
index 6ec499781f..9f75a558ec 100644
--- a/challenge-145/pokgopun/go/ch-2.go
+++ b/challenge-145/pokgopun/go/ch-2.go
@@ -1,12 +1,11 @@
// Write a script to create a Palindromic Tree for the given string.
-// All examples do not have a case that a letter happens more than twice in given words.
-// As a result, will initially ignore the case here.
+// All examples do not have a case that a letter happens more than twice in given words but we will handle the case as well
+// Here additional examples for the case: banana, redeemable, deterministic, heterogeneity, initiation, monopolous, honolulu
package main
import (
"fmt"
"os"
- "regexp"
"sort"
"strings"
)
@@ -23,6 +22,13 @@ func main() {
"challenge",
"champion",
"christmas",
+ "banana",
+ "redeemable",
+ "deterministic",
+ "heterogeneity",
+ "initiation",
+ "monopolous",
+ "honolulu",
}
}
for _, v := range sample {
@@ -34,47 +40,45 @@ func main() {
type pldt struct {
word string
seen map[string]bool
+ cpos map[byte][]int
+ vals []string
}
func newPldt(s string) (p pldt) {
p.word = s
p.seen = make(map[string]bool)
- return p
-}
-
-func (p pldt) find(b byte) (s []string) {
- for _, v := range regexp.MustCompile(string(b)+".*"+string(b)).FindAll([]byte(p.word), -1) {
- o := string(v)
- sort.SliceStable(v, func(i, j int) bool {
- return true
- })
- if o == string(v) {
- s = append(s, o)
- }
- //fmt.Println(string(v))
+ p.cpos = make(map[byte][]int)
+ for i, v := range []byte(p.word) {
+ p.cpos[v] = append(p.cpos[v], i)
}
- return s
-}
-
-func (p pldt) String() string {
- var b strings.Builder
- b.WriteString("Input: '" + p.word + "'\nOutput: ")
for _, c := range []byte(p.word) {
if p.seen[string(c)] {
continue
}
p.seen[string(c)] = true
- b.Write([]byte{c, ' '})
- /**/
- for _, v := range p.find(c) {
- if p.seen[v] {
- continue
+ p.vals = append(p.vals, string(c))
+ for i, pos1 := range p.cpos[c] {
+ for _, pos2 := range p.cpos[c][i+1:] {
+ o := p.word[pos1 : pos2+1]
+ if p.seen[o] {
+ continue
+ }
+ r := []byte(o)
+ sort.SliceStable(r, func(i, j int) bool {
+ return true
+ })
+ if o == string(r) {
+ p.vals = append(p.vals, o)
+ p.seen[o] = true
+ }
}
- p.seen[v] = true
- b.WriteString(v + " ")
}
- /**/
}
- b.WriteByte('\n')
+ return p
+}
+
+func (p pldt) String() string {
+ var b strings.Builder
+ b.WriteString("Input: '" + p.word + "'\nOutput: " + strings.Join(p.vals, " ") + "\n")
return b.String()
}