File tree Expand file tree Collapse file tree 3 files changed +45
-0
lines changed Expand file tree Collapse file tree 3 files changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public boolean isOneEditDistance (String s , String t ) {
3
+
4
+ char [] S = s .toCharArray ();
5
+ char [] T = t .toCharArray ();
6
+
7
+ // make sure s is always to shorter one
8
+ if (S .length > T .length ) return isOneEditDistance (t , s );
9
+
10
+ if (T .length - S .length > 1 ) return false ;
11
+
12
+ int diff = 0 ;
13
+
14
+ int i = 0 ;
15
+ int j = 0 ;
16
+
17
+ for (/* void */ ; i < S .length ; i ++, j ++){
18
+ if (S [i ] != T [j ]){
19
+
20
+ diff ++;
21
+
22
+ if (diff > 1 ) return false ;
23
+
24
+ // len(s) + 1 = len(t)
25
+ if (S .length != T .length && S [i ] == T [j + 1 ]){
26
+ j ++; // delete one from T
27
+ }
28
+ }
29
+ }
30
+
31
+ if (diff == 0 ){
32
+ return j + 1 == T .length ;
33
+ }
34
+
35
+ return j == T .length ;
36
+ }
37
+ }
Original file line number Diff line number Diff line change
1
+ ---
2
+ layout : solution
3
+ title : One Edit Distance
4
+ date : 2014-12-03 00:46:25+08:00
5
+ ---
6
+ {% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
7
+ {% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_ root/' }} %}
8
+ {% include {{leetcode_readme}} %}
You can’t perform that action at this time.
0 commit comments