r/dailyprogrammer • 1 2 • Jun 10 '13

[Easy] Longest Two-Character Sub-String

(Easy): Longest Two-Character Sub-String

This programming challenge is a classic interview question for software engineers: given a string, find the longest sub-string that contains, at most, two characters.

Author: /u/Regul

Formal Inputs & Outputs

Input Description

Through standard console input, you will be given a string to search, which only contains lower-case alphabet letters.

Output Description

Simply print the longest sub-string of the given string that contains, at most, two unique characters. If you find multiple sub-strings that match the description, print the last sub-string (furthest to the right).

Sample Inputs & Outputs

Sample Inputs

abbccc
abcabcabcabccc
qwertyytrewq

Sample Outputs

bbccc
bccc
tyyt
62 Upvotes

129 comments sorted by

View all comments

Show parent comment

3

u/leonardo_m Jun 11 '13

Your solution looks efficient. But I'd like it to store the best i and k, and perform a substr only at the end, to perform only one string copy.

This is a D port of your C++ code, with some changes because this is supposed to work with all kinds of Unicode text. (I have tried it with various strings, but I am not fully sure of its correctness, because Unicode is easy to get wrong when handled at low level, and despite the help of std.array methods that handle Unicode, to create 'result' I have used raw pointers.)

import std.stdio, std.array;

void main() {
    auto text = "hello\U00010143\u0100\U00010143";

    int longest = 0; // Length of the current longest solution.
    string result;
    auto r1 = text;
    int counter1 = 0;

    const end = !text.length ? null : &text[$ - 1] + 1;

    while (!r1.empty) {
        immutable c1 = r1.front;
        auto r2 = r1;
        int counter2 = counter1;

        while (!r2.empty && r2.front == c1) {
            r2.popFront;
            counter2++;
        }

        auto r3 = r2;
        int counter3 = counter2;

        if (!r3.empty) {
            immutable c2 = r3.front;
            while (!r3.empty && (r3.front == c1 || r3.front == c2)) {
                r3.popFront;
                counter3++;
            }
        }

        if (counter3 - counter1 >= longest) {
            longest = counter3 - counter1;
            result = text[r1.ptr - text.ptr .. r3.ptr - text.ptr];
        }

        r1 = r2;
        counter1 = counter2;
    }

    writeln("The longest relevant substring is: ", result);
}

In D I perform no copies of the string, just a slicing.

The counters are used because in Unicode text a pointer difference can't tell you the length of a string.

Each D string is 2 words long, so this wastes about 3 words in the stack frame, but this is not a problem. Most D standard library is designed to work on ranges, instead of single iterators. Designing a good Unicode iterator for this program is too much work.

This code also shows how quickly easy problem solutions become less easy if you want them to be both efficient, flexible and correct.

2

u/leonardo_m Jun 11 '13

A short D solution (a little inspired by skibo_ Python solution):

import std.stdio, std.range, std.algorithm;

string longestTwocharSubstr(in string s) {
    return iota(s.length + 1)
           .allPairs
           .map!(ij => s[ij[0] .. ij[1]])
           .filter!(p => p.set.length <= 2)
           .reduce!(max!len);
}

void main() {
    foreach (s; ["abbccc", "abcabcabcabccc", "qwertyytrewq"])
        writeln(s, " ", s.longestTwocharSubstr);
}

The library-like code it uses:

import std.range, std.typecons, std.traits;

struct AllPairs(Range) {
    alias R = Unqual!Range;
    alias Pair = Tuple!(ForeachType!R, "a", ForeachType!R, "b");
    R _input;
    size_t i, j;

    this(Range r_) {
        this._input = r_;
        j = 1;
    }

    @property bool empty() {
        return j >= _input.length;
    }

    @property Pair front() {
        return Pair(_input[i], _input[j]);
    }

    void popFront() {
        if (j >= _input.length - 1) {
            i++;
            j = i + 1;
        } else {
            j++;
        }
    }
}

AllPairs!Range allPairs(Range)(Range r)
if (isRandomAccessRange!Range && hasLength!Range && !isNarrowString!Range) {
    return typeof(return)(r);
}

// ----------------------

bool[ForeachType!R] set(R)(R items) if (isInputRange!R) {
    typeof(return) result;
    foreach (x; items)
        result[x] = true;
    return result;
}

template max(alias key) {
    T max(T)(T x, T y) {
        return (key(x) >= key(y)) ? x : y;
    }
}

auto len(R)(R items) {
    return items.length;
}

If you don't want to define a "set" you can use a sort:

.filter!(p => p.dup.representation.sort().uniq.walkLength <= 2)

1

u/oasisguy Jun 11 '13

You're right, there's no need to call substr() but once, in the end. Thanks for pointing it out!