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

Commit51f67fc

Browse files
add 647
1 parent5cbbbeb commit51f67fc

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_647.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,32 @@ private int extendPalindrome(String s, int left, int right) {
2525
returncount;
2626
}
2727
}
28+
29+
publicstaticclassSolution2 {
30+
/**
31+
* Simple brute force solution is accepted as well, although not ideal in terms of time complexity: O(n^2)
32+
*/
33+
publicintcountSubstrings(Strings) {
34+
intresult =0;
35+
for (inti =0;i <s.length();i++) {
36+
for (intj =i +1;j <=s.length();j++) {
37+
if (isPal(s.substring(i,j))) {
38+
result++;
39+
}
40+
}
41+
}
42+
returnresult;
43+
}
44+
45+
privatebooleanisPal(Stringstr) {
46+
inti =0;
47+
intj =str.length() -1;
48+
while (i <j) {
49+
if (str.charAt(i++) !=str.charAt(j--)) {
50+
returnfalse;
51+
}
52+
}
53+
returntrue;
54+
}
55+
}
2856
}

‎src/test/java/com/fishercoder/_647Test.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88

99
publicclass_647Test {
1010
privatestatic_647.Solution1solution1;
11+
privatestatic_647.Solution2solution2;
1112

1213
@BeforeClass
1314
publicstaticvoidsetup() {
1415
solution1 =new_647.Solution1();
16+
solution2 =new_647.Solution2();
1517
}
1618

1719
@Test
1820
publicvoidtest1() {
1921
assertEquals(3,solution1.countSubstrings("abc"));
22+
assertEquals(3,solution2.countSubstrings("abc"));
2023
}
2124

2225
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp