How to find repeated string?

190 views
Skip to first unread message

洪嘉鴻

unread,
Aug 19, 2020, 4:07:21 AM8/19/20
to golang-nuts
Hello everyone:
I use golang with Win10. The version of golang which I'm using is go1.15.
I want to find out the repeated string.
For example, given a string which is "1134534534534534".
I want the output string to be "345"
I've tried to 
Could anyone help me to solve this problem?

Any help is appreciated.
Thank you very much!
Max

Jan Mercl

unread,
Aug 19, 2020, 4:31:09 AM8/19/20
to 洪嘉鴻, golang-nuts
On Wed, Aug 19, 2020 at 10:07 AM 洪嘉鴻 <max1...@gmail.com> wrote:

> Hello everyone:
> I use golang with Win10. The version of golang which I'm using is go1.15.
> I want to find out the repeated string.
> For example, given a string which is "1134534534534534".
> I want the output string to be "345"
> I've tried to
> Could anyone help me to solve this problem?

It would be nice if you can show the code that you wrote in an attempt
to solve this problem. Someone may then point out where a mistake was
made and/or suggest a better solution.

洪嘉鴻

unread,
Aug 19, 2020, 5:41:57 AM8/19/20
to golang-nuts
Hello!
This is the code which I'm trying.
The generation of map is the candidates from the string s1.
However, the answer should be "345", which the count is not the most.

Thanks for your replying.
Max


Jan Mercl於 2020年8月19日星期三 UTC+8下午4時31分09秒寫道:

Brian Candler

unread,
Aug 19, 2020, 6:47:42 AM8/19/20
to golang-nuts
On Wednesday, 19 August 2020 10:41:57 UTC+1, 洪嘉鴻 wrote:
Hello!
This is the code which I'm trying.
The generation of map is the candidates from the string s1.
However, the answer should be "345", which the count is not the most.

Your program says that the string "345" occurs 4 times - which is correct.

However, it also says that there are other sequences which occur more times - e.g. "34" occurs 5 times.  And there are longer repeating patterns which occur fewer times, e.g. "3453" occurs 2 times.  These are all correct.

So you have to think: what is your reason that "345" should be selected as "the best" answer, given that there are other answers which are better in terms of number of repeats or length of sequence?

You could create a score which is the length of the string times the number of repeats - e.g. "345" length 3 x 4 occurrences = score 12:

But then there are non-repeating sequences which score higher.  Do you want to exclude sequences with a repeat count of 1 ?

Brian Candler

unread,
Aug 19, 2020, 6:54:01 AM8/19/20
to golang-nuts
Or another thought - perhaps you are looking for *adjacent* repeats only?

In that case strings.Count doesn't help because it counts all occurrences, not just adjacent ones.

Ian Davis

unread,
Aug 19, 2020, 7:03:35 AM8/19/20
to golan...@googlegroups.com


On Wed, 19 Aug 2020, at 11:54 AM, Brian Candler wrote:
Or another thought - perhaps you are looking for *adjacent* repeats only?

In that case strings.Count doesn't help because it counts all occurrences, not just adjacent ones.

I suspect the OP is looking for the longest repeating substring.

Ian Davis

unread,
Aug 19, 2020, 7:04:22 AM8/19/20
to golan...@googlegroups.com
Hmm, that's not correct. Sorry for the noise.

Michael Jones

unread,
Aug 19, 2020, 8:12:39 AM8/19/20
to Ian Davis, golang-nuts

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/bbc89836-e955-45ed-aaa5-f06669128281%40www.fastmail.com.


--
Michael T. Jones
michae...@gmail.com

Bakul Shah

unread,
Aug 19, 2020, 11:22:45 AM8/19/20
to 洪嘉鴻, golang-nuts
On Aug 19, 2020, at 1:07 AM, 洪嘉鴻 <max1...@gmail.com> wrote:
>
> Hello everyone:
> I use golang with Win10. The version of golang which I'm using is go1.15.
> I want to find out the repeated string.
> For example, given a string which is "1134534534534534".
> I want the output string to be "345"
> I've tried to
> Could anyone help me to solve this problem?

Your problem specification is ambiguous. For example, if you may be able
represent your input string as

...S^N... or
...T^M... or
...U...V..U..V..V.. where there are U occurs L times and V occurs K times.
Here S, T, U and V are substrings

Then any of the following possible answers may make sense:
1. S if len(S) > len(any T)
2. T if M > N (T occurs more frequently than any S)
3. U if len(U) > len(any V)
4. V if K > L (V occurs more frequently than any U)

Based on your example it appears you want 2. or 4. but can't be sure.
Specify the problem clearly & completely and the solution will usually
suggest itself!

Brian Candler

unread,
Aug 19, 2020, 4:27:13 PM8/19/20
to golang-nuts
Here's one way to do adjacent repeats.
https://play.golang.org/p/402UEPtIkfw

But if you don't understand regular expressions, this could be done more simply by:

- finding the first occurrence of substr
- advancing the offset to just after the end of the match
- check if substr appears at the new position
- keep repeating until substr cannot be found any more

洪嘉鴻

unread,
Aug 20, 2020, 4:45:12 AM8/20/20
to golang-nuts
Hello everyone,
I'll try the methods mentioned above.
Reply all
Reply to author
Forward
0 new messages