File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {number }
4
+ */
5
+
6
+ /**
7
+ * Runtime: 1521ms, Memory: 56.61MB
8
+ *
9
+ * Time complexity: O(N^2)
10
+ * Space complexity: O(N^2)
11
+ *
12
+ * Note: necessary to think of an alternative approach
13
+ * **/
14
+
15
+ function isPalindrome ( subString ) {
16
+ const len = subString . length ;
17
+ for ( let i = 0 ; i < len / 2 ; i ++ ) {
18
+ if ( subString [ i ] !== subString [ len - 1 - i ] ) {
19
+ return false ;
20
+ }
21
+ return true ;
22
+ }
23
+ }
24
+
25
+ var countSubstrings = function ( s ) {
26
+ const n = s . length ;
27
+ let answer = n ;
28
+
29
+ for ( let i = 0 ; i < n ; i ++ ) {
30
+ for ( let j = i + 1 ; j < n ; j ++ ) {
31
+ let subString = s . slice ( i , j + 1 ) ;
32
+ if ( isPalindrome ( subString ) ) {
33
+ answer += 1 ;
34
+ }
35
+ }
36
+ }
37
+
38
+ return answer ;
39
+ } ;
You can’t perform that action at this time.
0 commit comments