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

Commit3c00860

Browse files
authored
Added Check if One String Swap Can Make Strings Equal.java
1 parenteb2a473 commit3c00860

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
classSolution {
2+
publicfinalintUNSET_INDEX = -1;
3+
publicfinalintSET_INDEX =101;
4+
publicbooleanareAlmostEqual(Strings1,Strings2) {
5+
intswapIndex =UNSET_INDEX;
6+
for (inti =0;i <s1.length();i++) {
7+
if (s1.charAt(i) !=s2.charAt(i)) {
8+
if (swapIndex ==SET_INDEX) {
9+
returnfalse;
10+
}elseif (swapIndex ==UNSET_INDEX) {
11+
swapIndex =i;
12+
}else {
13+
if (!(s1.charAt(swapIndex) ==s2.charAt(i) &&s1.charAt(i) ==s2.charAt(swapIndex))) {
14+
returnfalse;
15+
}
16+
swapIndex =SET_INDEX;
17+
}
18+
}
19+
}
20+
returnswapIndex ==UNSET_INDEX ||swapIndex ==SET_INDEX;
21+
}
22+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp