One Edit DistanceTotal accepted:
709Total Submissions:
3097
Given two strings S and T, determine if they are both one edit distance apart.
Analysis
Delete, add, replace three are all one edit distance. The delete and add two string lengths are 1 and replace the same length.
Attention
None
[CODE]
public class Solution {public Boolean isoneeditdistance (string s, String t) {if (S==null | | | t==null) return f
Alse;
if (Math.Abs (S.length ()-t.length ()) >1) {return false;
} if (s.length () = = T.length ()) {int count = 0;
for (int i=0; i<s.length (); i++) {if (S.charat (i)!= T.charat (i)) ++count;
if (Count > 1) return false;
return count==1;
else {String ss,ls;
if (S.length () < T.length ()) {SS = s;
ls = t;
else {ss = t;
ls = s;
int i=0, j=0;
int count = 0;
while (I<ss.length () && j<ls.length ()) {if (Ss.charat (i)!= Ls.charat (j)) { ++count;
++j;
if (Count > 1) return false;
} else { ++i;
++j;
} return true; }
}
}