Skip to main content
typo
Source Link
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65
  1. Using a size_t for a boolean flag, and calling it flag, is nearly an obfuscation. Use int pre-C99 and call it something descriptive like substitute.

  2. strlen() is likely a waste, though it makes describing the algorithm easier. Try to do without the additional iteration.

  3. Getting the length of the longest common prefix looks like a well-defined task which can be extracted, and a useful building-block. Do so.

  4. one_way, one_away, or one_microsoft_way? Having a properly-spelled correctly-selected name is Always very important.

  5. Change to a single-traversal algorithm:

Remove hethe common prefix.

Measuring the common prefix without the first characters is quite instructive.

With `x` and `y` different from `a`, call `n = strlen_common(++a, ++b)`:

1. At most one substitution at the start:

        xaaaabcde
        yaaaabcde

    Result n == 8, a[n] == b[n].

1. Deletion from the first:

        xaaaabcde
        aaaabcde

    n == 3, strcmp(a + n, b + n - 1) != 0

1. Same way for deletion from second.

The modified code (also live on coliru):

size_t strlen_common(const char* a, const char* b) {
    size_t r = 0;
    while (*a && *a++ == *b++)
        ++r;
    return r;
}

int one_away(const char* a, const char* b) {
    size_t n = strlen_common(a, b);
    a += n;
    b += n;
    if (!*a*a++)
        return !*b || !b[1];
    if (!*b*b++)
        return !a[1];*a;
    n = strlen_common(++aa, ++bb);
    return a[n] == b[n]
        || !strcmp(a + n - 1, b + n)
        || !strcmp(a + n, b + n - 1);
}
  1. Using a size_t for a boolean flag, and calling it flag, is nearly an obfuscation. Use int pre-C99 and call it something descriptive like substitute.

  2. strlen() is likely a waste, though it makes describing the algorithm easier. Try to do without the additional iteration.

  3. Getting the length of the longest common prefix looks like a well-defined task which can be extracted, and a useful building-block. Do so.

  4. one_way, one_away, or one_microsoft_way? Having a properly-spelled correctly-selected name is Always very important.

  5. Change to a single-traversal algorithm:

Remove he common prefix.

Measuring the common prefix without the first characters is quite instructive.

With `x` and `y` different from `a`, call `n = strlen_common(++a, ++b)`:

1. At most one substitution at the start:

        xaaaabcde
        yaaaabcde

    Result n == 8, a[n] == b[n].

1. Deletion from the first:

        xaaaabcde
        aaaabcde

    n == 3, strcmp(a + n, b + n - 1) != 0

1. Same way for deletion from second.

The modified code (also live on coliru):

size_t strlen_common(const char* a, const char* b) {
    size_t r = 0;
    while (*a && *a++ == *b++)
        ++r;
    return r;
}

int one_away(const char* a, const char* b) {
    size_t n = strlen_common(a, b);
    a += n;
    b += n;
    if (!*a)
        return !*b || !b[1];
    if (!*b)
        return !a[1];
    n = strlen_common(++a, ++b);
    return a[n] == b[n]
        || !strcmp(a + n - 1, b + n)
        || !strcmp(a + n, b + n - 1);
}
  1. Using a size_t for a boolean flag, and calling it flag, is nearly an obfuscation. Use int pre-C99 and call it something descriptive like substitute.

  2. strlen() is likely a waste, though it makes describing the algorithm easier. Try to do without the additional iteration.

  3. Getting the length of the longest common prefix looks like a well-defined task which can be extracted, and a useful building-block. Do so.

  4. one_way, one_away, or one_microsoft_way? Having a properly-spelled correctly-selected name is Always very important.

  5. Change to a single-traversal algorithm:

Remove the common prefix.

Measuring the common prefix without the first characters is quite instructive.

With `x` and `y` different from `a`, call `n = strlen_common(++a, ++b)`:

1. At most one substitution at the start:

        xaaaabcde
        yaaaabcde

    Result n == 8, a[n] == b[n].

1. Deletion from the first:

        xaaaabcde
        aaaabcde

    n == 3, strcmp(a + n, b + n - 1) != 0

1. Same way for deletion from second.

The modified code (also live on coliru):

size_t strlen_common(const char* a, const char* b) {
    size_t r = 0;
    while (*a && *a++ == *b++)
        ++r;
    return r;
}

int one_away(const char* a, const char* b) {
    size_t n = strlen_common(a, b);
    a += n;
    b += n;
    if (!*a++)
        return !*b || !b[1];
    if (!*b++)
        return !*a;
    n = strlen_common(a, b);
    return a[n] == b[n]
        || !strcmp(a + n - 1, b + n)
        || !strcmp(a + n, b + n - 1);
}
deleted 18 characters in body
Source Link
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65
  1. Using a size_t for a boolean flag, and calling it flag, is nearly an obfuscation. Use int pre-C99 and call it something descriptive like substitute.

  2. strlen() is likely a waste, though it makes describing the algorithm easier. Try to do without the additional iteration.

  3. Getting the length of the longest common prefix looks like a well-defined task which can be extracted, and a useful building-block. Do so.

  4. one_way, one_away, or one_microsoft_way? Having a properly-spelled correctly-selected name is Always very important.

  5. Change to a single-traversal algorithm:

Remove he common prefix.

Measuring the common prefix without the first characters is quite instructive.

With `x` and `y` different from `4`, ignore space`a`, call `n = strlen_common(++a, ++b)`:

1. At most one substitution at the start:

        x4444 0123xaaaabcde
        y4444 0123yaaaabcde

    Result n == 8, a[n] == b[n].

1. Deletion from the first:

        x4444_0123xaaaabcde
        4444_0123aaaabcde

    n == 3, strcmp(a + n, b + n - 1) != 0

1. Same way for deletion from second.

The modified code (also live on coliru):

size_t strlen_common(const char* a, const char* b) {
    size_t r = 0;
    while (*a && *a++ == *b++)
        ++r;
    return r;
}

int one_away(const char* a, const char* b) {
    size_t n = strlen_common(a, b);
    a += n;
    b += n;
    if (!*a)
        return !*b || !b[1];
    if (!*b)
        return !a[1];
    n = strlen_common(++a, ++b);
    return a[n] == b[n]
        || !strcmp(a + n - 1, b + n)
        || !strcmp(a + n, b + n - 1);
}
  1. Using a size_t for a boolean flag, and calling it flag, is nearly an obfuscation. Use int pre-C99 and call it something descriptive like substitute.

  2. strlen() is likely a waste, though it makes describing the algorithm easier. Try to do without the additional iteration.

  3. Getting the length of the longest common prefix looks like a well-defined task which can be extracted, and a useful building-block. Do so.

  4. one_way, one_away, or one_microsoft_way? Having a properly-spelled correctly-selected name is Always very important.

  5. Change to a single-traversal algorithm:

Remove he common prefix.

Measuring the common prefix without the first characters is quite instructive.

With `x` and `y` different from `4`, ignore space, call `n = strlen_common(++a, ++b)`:

1. At most one substitution at the start:

        x4444 0123
        y4444 0123

    Result n == 8, a[n] == b[n].

1. Deletion from the first:

        x4444_0123
        4444_0123

    n == 3, strcmp(a + n, b + n - 1) != 0

1. Same way for deletion from second.

The modified code (also live on coliru):

size_t strlen_common(const char* a, const char* b) {
    size_t r = 0;
    while (*a && *a++ == *b++)
        ++r;
    return r;
}

int one_away(const char* a, const char* b) {
    size_t n = strlen_common(a, b);
    a += n;
    b += n;
    if (!*a)
        return !*b || !b[1];
    if (!*b)
        return !a[1];
    n = strlen_common(++a, ++b);
    return a[n] == b[n]
        || !strcmp(a + n - 1, b + n)
        || !strcmp(a + n, b + n - 1);
}
  1. Using a size_t for a boolean flag, and calling it flag, is nearly an obfuscation. Use int pre-C99 and call it something descriptive like substitute.

  2. strlen() is likely a waste, though it makes describing the algorithm easier. Try to do without the additional iteration.

  3. Getting the length of the longest common prefix looks like a well-defined task which can be extracted, and a useful building-block. Do so.

  4. one_way, one_away, or one_microsoft_way? Having a properly-spelled correctly-selected name is Always very important.

  5. Change to a single-traversal algorithm:

Remove he common prefix.

Measuring the common prefix without the first characters is quite instructive.

With `x` and `y` different from `a`, call `n = strlen_common(++a, ++b)`:

1. At most one substitution at the start:

        xaaaabcde
        yaaaabcde

    Result n == 8, a[n] == b[n].

1. Deletion from the first:

        xaaaabcde
        aaaabcde

    n == 3, strcmp(a + n, b + n - 1) != 0

1. Same way for deletion from second.

The modified code (also live on coliru):

size_t strlen_common(const char* a, const char* b) {
    size_t r = 0;
    while (*a && *a++ == *b++)
        ++r;
    return r;
}

int one_away(const char* a, const char* b) {
    size_t n = strlen_common(a, b);
    a += n;
    b += n;
    if (!*a)
        return !*b || !b[1];
    if (!*b)
        return !a[1];
    n = strlen_common(++a, ++b);
    return a[n] == b[n]
        || !strcmp(a + n - 1, b + n)
        || !strcmp(a + n, b + n - 1);
}
added 77 characters in body
Source Link
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65
  1. Using a size_t for a boolean flag, and calling it flag, is nearly an obfuscation. Use int pre-C99 and call it something descriptive like substitute.

  2. strlen() is likely a waste, though it makes describing the algorithm easier. Try to do without the additional iteration.

  3. Getting the length of the longest common prefix looks like a well-defined task which can be extracted, and a useful building-block. Do so.

  4. one_way, one_away, or one_microsoft_way? Having a properly-spelled correctly-selected name is Always very important.

  5. Change to a single-traversal algorithm:

Remove he common prefix.

Measuring the common prefix without the first characters is quite instructive.

With `x` and `y` different from `4`, ignore space, call `n = strlen_common(++a, ++b)`:

1. At most one substitution at the start:

        x4444 0123
        y4444 0123

    Result n == 8, a[n] == b[n].

1. Deletion from the first:

        x4444_0123
        4444_0123

    n == 3, strcmp(a + n, b + n - 1) != 0

1. Same way for deletion from second.

The modified code (also live on coliru):

size_t strlen_common(const char* a, const char* b) {
    size_t r = 0;
    while (*a && *a++ == *b++)
        ++r;
    return r;
}

boolint one_away(const char* a, const char* b) {
    size_t n = strlen_common(a, b);
    a += n;
    b += n;
    if (!*a)
        return !*b || !b[1];
    if (!*b)
        return !a[1];
    n = strlen_common(++a, ++b);
    return a[n] == b[n]
        || !strcmp(a + n - 1, b + n)
        || !strcmp(a + n, b + n - 1);
}
  1. Using a size_t for a boolean flag, and calling it flag, is nearly an obfuscation. Use int pre-C99 and call it something descriptive like substitute.

  2. strlen() is likely a waste, though it makes describing the algorithm easier. Try to do without the additional iteration.

  3. Getting the length of the longest common prefix looks like a well-defined task which can be extracted, and a useful building-block. Do so.

  4. one_way, one_away, or one_microsoft_way? Having a properly-spelled correctly-selected name is Always very important.

  5. Change to a single-traversal algorithm:

Remove he common prefix.

Measuring the common prefix without the first characters is quite instructive.

With `x` and `y` different from `4`, ignore space, call `n = strlen_common(++a, ++b)`:

1. At most one substitution at the start:

        x4444 0123
        y4444 0123

    Result n == 8, a[n] == b[n].

1. Deletion from the first:

        x4444_0123
        4444_0123

    n == 3, strcmp(a + n, b + n - 1) != 0

1. Same way for deletion from second.

The modified code:

size_t strlen_common(const char* a, const char* b) {
    size_t r = 0;
    while (*a && *a++ == *b++)
        ++r;
    return r;
}

bool one_away(const char* a, const char* b) {
    size_t n = strlen_common(a, b);
    a += n;
    b += n;
    if (!*a)
        return !*b || !b[1];
    if (!*b)
        return !a[1];
    n = strlen_common(++a, ++b);
    return a[n] == b[n]
        || !strcmp(a + n - 1, b + n)
        || !strcmp(a + n, b + n - 1);
}
  1. Using a size_t for a boolean flag, and calling it flag, is nearly an obfuscation. Use int pre-C99 and call it something descriptive like substitute.

  2. strlen() is likely a waste, though it makes describing the algorithm easier. Try to do without the additional iteration.

  3. Getting the length of the longest common prefix looks like a well-defined task which can be extracted, and a useful building-block. Do so.

  4. one_way, one_away, or one_microsoft_way? Having a properly-spelled correctly-selected name is Always very important.

  5. Change to a single-traversal algorithm:

Remove he common prefix.

Measuring the common prefix without the first characters is quite instructive.

With `x` and `y` different from `4`, ignore space, call `n = strlen_common(++a, ++b)`:

1. At most one substitution at the start:

        x4444 0123
        y4444 0123

    Result n == 8, a[n] == b[n].

1. Deletion from the first:

        x4444_0123
        4444_0123

    n == 3, strcmp(a + n, b + n - 1) != 0

1. Same way for deletion from second.

The modified code (also live on coliru):

size_t strlen_common(const char* a, const char* b) {
    size_t r = 0;
    while (*a && *a++ == *b++)
        ++r;
    return r;
}

int one_away(const char* a, const char* b) {
    size_t n = strlen_common(a, b);
    a += n;
    b += n;
    if (!*a)
        return !*b || !b[1];
    if (!*b)
        return !a[1];
    n = strlen_common(++a, ++b);
    return a[n] == b[n]
        || !strcmp(a + n - 1, b + n)
        || !strcmp(a + n, b + n - 1);
}
Source Link
Deduplicator
  • 19.3k
  • 1
  • 31
  • 65
Loading