32
\$\begingroup\$

Inspired by Is it double speak?, I devised a harder challenge. Given a string, determine if the string is n-speak, for any \$n\geq 2\$.

N-speak is defined by repeating each letter \$n\$ times. With \$n = 4\$, the string Hello is transformed to HHHHeeeelllllllloooo. Your goal is to figure out if the input is a valid output for any n-speak transformation.

It should be noted that any sentence which is valid n-speak, for \$n = 2k\$, is also valid k-speak. Thus, the hard parts to solve will be odd values of \$n\$.

Input

A string consisting of at least 2 characters. Input could also be a list of characters. Input is case sensitive.

Output

Truthy if the string is n-speak, falsey otherwise.

Examples

True cases

HHeelllloo,,  wwoorrlldd!!
TTTrrriiipppllleee   ssspppeeeaaakkk
QQQQuuuuaaaaddddrrrruuuupppplllleeee    ssssppppeeeeaaaakkkk
7777777-------ssssssspppppppeeeeeeeaaaaaaakkkkkkk
999999999
aaaabb
aaaaaaaabbbbcc
aaaaabbbbb
@@@

If you want to generate additional truthy cases, you can use this MathGolf script. Place the string within the quotation marks, and the value of \$n\$ as the input.

False cases

Hello, world!
TTTrrriiipppllleee   speak
aaaaaaaaaaaaaaaab
Ddoouubbllee  ssppeeaakk
aabbab
aaaabbb
a (does not need to be handled)
(empty string, does not need to be handled)

Of course, since this is code golf, get ready to trim some bytes!

\$\endgroup\$
  • \$\begingroup\$ Suggested test case: aabbab \$\endgroup\$ – Adám Aug 12 at 15:22
  • \$\begingroup\$ Suggested test case: aaaabbb \$\endgroup\$ – 640KB Aug 12 at 15:37
  • \$\begingroup\$ I'll add them both tomorrow, good suggestions. \$\endgroup\$ – maxb Aug 12 at 21:12
  • 4
    \$\begingroup\$ I am genuinely honoured and flattered that you have used and expanded my challenge :) \$\endgroup\$ – AJFaraday Aug 13 at 9:46
  • \$\begingroup\$ @AJFaraday glad that you liked it! I enjoyed both of your challenges, which gave me the idea for this one. There might be an even harder challenge coming soon. \$\endgroup\$ – maxb Aug 13 at 10:25

33 Answers 33

1
\$\begingroup\$

Gaia, 10 bytes

ẋl¦d¦&⊢⌉1>

Try it online!

Same "GCD of run-lengths > 1" as many other submissions use.

There is a bug in ė (run-length encoding) that drops the last unique element of the list, otherwise we could have the following 9 byte solution: ė(¦d¦&⊢1>.

\$\endgroup\$
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 76 bytes

s=>s.Where((_,n)=>s.Count%(n+=2)<1&!s.Where((c,i)=>c!=s[i/n*n]).Any()).Any()

Try it online!

Generate all n from 2, 3, 4, ... and do the following:

  • Check if the length of input is divisible by n
  • Compare each character of input to the corresponding character of an n-speak string

If both checks pass, the input string is n-speak.

\$\endgroup\$
1
\$\begingroup\$

D , 126 bytes

bool f(string s){import std.algorithm;auto a=s.group.minElement!(a=>a[1])[1];foreach(g;s.group)if(g[1]%a)return 0;return a>1;}

First code golf I've done in D.

Does not handle empty input strings (causes an assertion failure in the standard library).

Ungolfed version:

bool f(string s) {
    import std.algorithm; // for group and minElement

    // splits the string into a groups of the same character
    // eg. "HHHiii".group returns
    // [Tuple!(char, uint)('H', 3), Tuple!(char, uint)('i', 3)]
    auto groups = s.group;

    // gets the smallest group length
    auto min_length = groups.minElement!(a => a[1])[1];

    foreach(g; groups) // for each group
        if(g[1] % min_length) // if it's length is not divisible by smallest length
            return 0; // return false

    return min_length > 1; // return true if smallest length was above 1
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.