Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6cc7efc

Browse files
MEDIUM/src/medium/OneEditDistance.java
1 parent29e3537 commit6cc7efc

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
packagemedium;
2+
3+
publicclassOneEditDistance {
4+
5+
publicstaticbooleanisOneEditDistance(Strings,Stringt) {
6+
char[]schar =s.toCharArray();
7+
char[]tchar =t.toCharArray();
8+
9+
if(Math.abs(s.length() -t.length()) ==1){
10+
char[]longer = (s.length() >t.length()) ?schar :tchar;
11+
char[]shorter = (longer ==schar) ?tchar :schar;
12+
13+
intdiffCnt =0,i =0,j =0;
14+
for(;i <shorter.length &&j <longer.length;){
15+
if(longer[j] !=shorter[i]){
16+
diffCnt++;
17+
j++;
18+
}else {
19+
i++;
20+
j++;
21+
}
22+
}
23+
returndiffCnt ==1 ||diffCnt ==0;//it could be the last char of the longer is the different one, in that case, diffCnt remains to be zero
24+
}elseif(s.length() ==t.length()) {
25+
intdiffCnt =0;
26+
for(inti =0;i <s.length();i++){
27+
if(schar[i] !=tchar[i]) {
28+
diffCnt++;
29+
}
30+
if(diffCnt >1)returnfalse;
31+
}
32+
returndiffCnt ==1;
33+
}
34+
returnfalse;
35+
}
36+
37+
publicstaticvoidmain(String...strings){
38+
Strings ="a";
39+
Stringt ="ac";
40+
System.out.println(isOneEditDistance(s,t));
41+
}
42+
43+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp