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\$.


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


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


True cases

HHeelllloo,,  wwoorrlldd!!
TTTrrriiipppllleee   ssspppeeeaaakkk
QQQQuuuuaaaaddddrrrruuuupppplllleeee    ssssppppeeeeaaaakkkk

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
Ddoouubbllee  ssppeeaakk
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!

  • \$\begingroup\$ Suggested test case: aabbab \$\endgroup\$
    – Adám
    Commented Aug 12, 2019 at 15:22
  • \$\begingroup\$ Suggested test case: aaaabbb \$\endgroup\$
    – 640KB
    Commented Aug 12, 2019 at 15:37
  • \$\begingroup\$ I'll add them both tomorrow, good suggestions. \$\endgroup\$
    – maxb
    Commented Aug 12, 2019 at 21:12
  • 4
    \$\begingroup\$ I am genuinely honoured and flattered that you have used and expanded my challenge :) \$\endgroup\$
    – AJFaraday
    Commented Aug 13, 2019 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
    Commented Aug 13, 2019 at 10:25

33 Answers 33


Gaia, 10 bytes


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>.


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


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.


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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