diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-06-08 13:42:45 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-08 13:42:45 +0100 |
| commit | fbe6534dfbb8a2899f6ff97043d11a64be7b49ba (patch) | |
| tree | e9df9e569b772ffe42a2551d985ecd276c4b4237 | |
| parent | b8d9da775e3c046045a8197efe7273d2d053ef75 (diff) | |
| parent | 0709228ea1c882a9d1c578af1ca52a2ca695539c (diff) | |
| download | perlweeklychallenge-club-fbe6534dfbb8a2899f6ff97043d11a64be7b49ba.tar.gz perlweeklychallenge-club-fbe6534dfbb8a2899f6ff97043d11a64be7b49ba.tar.bz2 perlweeklychallenge-club-fbe6534dfbb8a2899f6ff97043d11a64be7b49ba.zip | |
Merge pull request #6227 from pokgopun/pwc145
pwc145 solution in go - ch-2 to handle the case where a letter happen…
| -rw-r--r-- | challenge-145/pokgopun/go/ch-2.go | 66 |
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() } |
