We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cb7b808 commit 4ec527eCopy full SHA for 4ec527e
palindromic-substrings/njngwn.java
@@ -0,0 +1,36 @@
1
+// Time Complexity: O(n*n), n: s.length()
2
+// Space Complexity: O(1)
3
+class Solution {
4
+ public int countSubstrings(String s) {
5
+ int cnt = 0;
6
+
7
+ for (int i = 0; i < s.length(); i++, cnt++) { // i: center of palindrom
8
+ // odd number palindrom: e.g. aba
9
+ int left = i-1, right = i+1;
10
11
+ while (left >= 0 && right < s.length()) {
12
+ if (s.charAt(left) != s.charAt(right)) {
13
+ break;
14
+ }
15
+ cnt++;
16
+ left--;
17
+ right++;
18
19
20
+ // even number palindrom: e.g. abba
21
+ left = i;
22
+ right = i+1;
23
24
25
26
27
28
29
30
31
32
33
34
+ return cnt;
35
36
+}
0 commit comments