Skip to content

Commit

Permalink
Palindromic Substrings
Browse files Browse the repository at this point in the history
  • Loading branch information
hyejjun committed Aug 16, 2024
1 parent b776697 commit 1d1ceca
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions palindromic-substrings/hyejjun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @param {string} s
* @return {number}
*/
var countSubstrings = function (s) {
let count = 0;

function checkPalindromic(left, right) {
while (left >= 0 && right < s.length && s[left] === s[right]) {
count++;
left--;
right++;
}

}

for (let i = 0; i < s.length; i++) {
checkPalindromic(i, i);
checkPalindromic(i, i + 1);
}

return count;
};

console.log(countSubstrings("abc"));
console.log(countSubstrings("aaa"));


/*
Time Complexity : O(n^2)
Space Complexity: O(1)
*/

0 comments on commit 1d1ceca

Please sign in to comment.