File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @return {number }
4+ *
5+ * ์๊ฐ๋ณต์ก๋: O(n^2)
6+ * - ๊ฐ ๋ฌธ์(i)๋ฅผ ์ค์ฌ์ผ๋ก ํ์ฅ โ ์ต๋ n๋ฒ
7+ * - ๊ฐ ์ค์ฌ์์ ์์ชฝ์ผ๋ก ํ์ฅํ๋ฉด์ ๋น๊ต โ ์ต๋ n๋ฒ
8+ * โ O(n) * O(n) = O(n^2)
9+ *
10+ * ๊ณต๊ฐ๋ณต์ก๋: O(1)
11+ * - ์ถ๊ฐ ๊ณต๊ฐ ์์ด count ๋ณ์์ index๋ง ์ฌ์ฉ
12+ * - ๋ฌธ์์ด์ ๋ณต์ฌํ๊ฑฐ๋ ๋ฉ๋ชจ์ด์ ์ด์
ํ์ง ์์
13+ */
14+ var countSubstrings = function ( s ) {
15+ let count = 0 ;
16+
17+ // ์ ์ฒด ๋ฌธ์์ด์ ์ํํ๋ฉด์ ๊ฐ ์์น๋ฅผ ํฐ๋ฆฐ๋๋กฌ ์ค์ฌ์ผ๋ก ์ก์
18+ for ( let i = 0 ; i < s . length ; i ++ ) {
19+ // ํ์ ๊ธธ์ด ํฐ๋ฆฐ๋๋กฌ: ex. "aba"
20+ helper ( i , i ) ;
21+
22+ // ์ง์ ๊ธธ์ด ํฐ๋ฆฐ๋๋กฌ: ex. "abba"
23+ helper ( i , i + 1 ) ;
24+ }
25+
26+ // ํฐ๋ฆฐ๋๋กฌ์ด ์ ์ง๋๋ ๋์ count ์ฆ๊ฐ์ํค๋ ํจ์
27+ function helper ( left , right ) {
28+ // ์กฐ๊ฑด์ด ๋ง์ ๋๋ง๋ค ์ข์ฐ๋ก ํ์ฅ
29+ while ( left >= 0 && right < s . length && s [ left ] === s [ right ] ) {
30+ count ++ ; // ์ ํจํ ํฐ๋ฆฐ๋๋กฌ ํ๋ ๋ฐ๊ฒฌ
31+ left -- ;
32+ right ++ ;
33+ }
34+ }
35+
36+ return count ;
37+ } ;
You canโt perform that action at this time.
0 commit comments