Skip to content

Commit 75b9fc1

Browse files
committed
one edit distance
1 parent c534437 commit 75b9fc1

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

one-edit-distance/README.md

Whitespace-only changes.

one-edit-distance/Solution.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

one-edit-distance/index.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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}} %}

0 commit comments

Comments
 (0)