http://www.mitbbs.com/article_t1/JobHunting/32906379_0_2.html

//there is some variable names are incorrect. corrected in the code below;


class IntFileIterator {
   boolean hasNext();
   int next();
}

class Solution
{
  public boolean isDistanceZeroOrOne(IntFileIterator a, IntFileIterator b) {
            boolean ins_a = false, ins_b = false, 
            replace = false, diff = false;
            int pre_a = 0;
            int pre_b = 0;
            while (a.hasNext() && b.hasNext()) {
                int cur_a = a.next(), cur_b = b.next();
                if (!ins_a && !ins_b && !replace) {
                    if (cur_a != cur_b) {
                        ins_a = ins_b = replace = diff = true;
                    }
                } else {
                    if (ins_b && pre_b != cur_a) ins_b = false;
                    if (ins_a && pre_a != cur_b) ins_a = false;
                    if (replace && cur_a != cur_b) replace = false;
                    if (!ins_a && !ins_b && !replace) return false;
                }
                pre_a = cur_a;
                pre_b = cur_b;
            }

            if (!a.hasNext() && !b.hasNext()) {
                return !diff || replace;
            } else if (a.hasNext()) {
                int cur_a = a.next();
                return (!diff || (ins_b && pre_b == cur_a)) && !cur_a.hasNext();
            } else if (b.hasNext()) {
                int cur_b = b.next();
                return (!diff || (ins_a && pre_a == cur_b)) && !cur_b.hasNext();
            }
            return false;
        }
    }
}
