Skip to content

Commit

Permalink
add: palindromic-substrings
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrineKim committed Jan 4, 2025
1 parent a7eff01 commit e051a94
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions palindromic-substrings/HerrineKim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 시간 복잡도: O(n^2)
// 공간 복잡도: O(1)

/**
* @param {string} s
* @return {number}
*/
var countSubstrings = function(s) {
let count = 0;

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

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

return count;
};

0 comments on commit e051a94

Please sign in to comment.