Skip to content

Commit 2fe91c1

Browse files
committed
Palindromic Substrings Solution
1 parent d2dc37f commit 2fe91c1

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

palindromic-substrings/naringst.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
};

0 commit comments

Comments
 (0)