Skip to main content
1 of 3
Thomas Dignan
  • 409
  • 1
  • 3
  • 11

Here is my own un-golfed answer in C. I'm sure someone can beat it:

#include <stdio.h>
#include <string.h>
#include <assert.h>

int tailrep(char *str, char *sub, int sublen, int len) {
    if (str == sub) {
        return 1;
    }

    int i;
    for (i = 0; i < sublen; i++) {
        char *a,  *b;
        a = str + len - sublen;
        b = sub + sublen - 1;
        
//        printf("%p->%c %p->%c\n", a - i, a[-i], b - i, b[-i]);
        if (a[-i] != b[-i]) {
            return 0;
        } 
    }
    
    return tailrep(str, sub - (sublen + 1), sublen + 1, len - sublen);
}

int istailrep(char *str) {
    int len = strlen(str);
    if (len < 3) {
        return 0;
    }
    return tailrep(str, (str + len - 1), 1, len - 1);
}

int main() {
    // must be false
    assert(istailrep("") == 0);
    assert(istailrep("a") == 0);
    assert(istailrep("aa") == 0);
    assert(istailrep("aab") == 0);
    assert(istailrep("abcbbc") == 0);
    assert(istailrep("abcdabcdabcd") == 0);

    // must be true
    assert(istailrep("aaa") == 1);
    assert(istailrep("abb") == 1);
    assert(istailrep("abcbcc") == 1);
    assert(istailrep("abcdbcdcdd") == 1);
    assert(istailrep("abcdebcdecdedee") == 1);
    printf("yay\n");
    return 0;
}
Thomas Dignan
  • 409
  • 1
  • 3
  • 11