Skip to content

Instantly share code, notes, and snippets.

@sturgle
Created December 14, 2014 02:22
Show Gist options
  • Save sturgle/bfbc314ff660f8d35b14 to your computer and use it in GitHub Desktop.
Save sturgle/bfbc314ff660f8d35b14 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
bool equal(string& s, int i, string& t, int j) {
while (i < s.size() && j < t.size() && s[i] == t[j]) {
i++;
j++;
}
//cout << "i: " << i << ", j: " << j << endl;
return (i == s.size() && j == t.size());
}
bool oneDist(string& s, string& t) {
int i = 0; int j = 0;
while (i < s.size() && j < t.size() && s[i] == t[j]) {
i++;
j++;
}
return equal(s, i, t, j) || equal(s, i+1, t, j+1) ||
equal(s, i+1, t, j) || equal(s, i, t, j+1);
}
void test(const char* a, const char *b, bool expected) {
string strA = a;
string strB = b;
//cout << strA << " vs. " << strB << endl;
assert(oneDist(strA, strB) == expected);
}
int main() {
test("abc", "abc", true);
test("abc", "abYc", true);
test("abXc", "abc", true);
test("abXc", "abYc", true);
test("abc", "abcY", true);
test("abcX", "abc", true);
test("Xabc", "abc", true);
test("abc", "Yabc", true);
test("aXbc", "abYc", false);
test("abc", "abcYYY", false);
test("abcXXX", "abc", false);
test("abcXXX", "abcYYY", false);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment