File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ package leetcode_study
2
+
3
+ /*
4
+ * ์ฃผ์ด์ง ๋ฌธ์์ด์์ ์๋ฅธ ๊ฐ๋ณ ๋ฌธ์์ด์ด ํ๋ฌธ์ผ ๊ฒฝ์ฐ๋ฅผ ํ๋จํ๋ ๋ฌธ์
5
+ * ์๊ฐ ๋ณต์ก๋: O(n^2)
6
+ * -> ์ฃผ์ด์ง ๋ฌธ์์ด์ ์๋ฅผ ๋ ์ด์ค ๋ฐ๋ณต๋ฌธ ์ํ: O(n^2)
7
+ * -> subString() ํจ์๋ ๋ด๋ถ์ ์ผ๋ก ์ฃผ์ด์ง k ๋งํผ ๋ฐฐ์ด์ ์๋ฅด๊ธฐ ๋๋ฌธ์ O(k)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง
8
+ * --> O(k) + O(n^2) = O(n^2)
9
+ * ๊ณต๊ฐ ๋ณต์ก๋: O(n)
10
+ * -> subString() ํจ์๊ฐ ํธ์ถ๋ ๋๋ง๋ค ๊ธธ์ด k์ ์๋ก์ด ๋ฌธ์์ด ๊ฐ์ฒด๊ฐ ์์ฑ๋๊ธฐ ๋๋ฌธ์ subString ์ต๋ ๊ธธ์ด์ธ O(n)์ ๊ฐ์ง.
11
+ * */
12
+ fun countSubstrings (s : String ): Int {
13
+ var count = 0
14
+
15
+ val maxLength = s.length
16
+ for (startIndex in 0 until maxLength) {
17
+ for (endIndex in startIndex + 1 .. maxLength) {
18
+ val temp = s.substring(startIndex, endIndex)
19
+ if (temp == temp.reversed()) {
20
+ count++
21
+ }
22
+ }
23
+ }
24
+ return count
25
+ }
26
+
27
+ /*
28
+ * ์๋ฅธ ๋ฌธ์์ด์ ๊ฐ๋ณ์ ์ผ๋ก ํ๋ฌธ์ธ์ง ํ๋จํด์ผํ๋๋ฐ ์ฃผ์ด์ง ๋ฌธ์์ด์์ ํฌํจํ๋ ๊ฒ์ผ๋ก ๋ฌธ์ ๋ฅผ ํด์ํ๊ณ
29
+ * ํด๊ฒฐํ๊ธฐ ๋๋ฌธ์ ํต๊ณผํ์ง ๋ชปํจ
30
+ * */
31
+ fun countSubstrings01 (s : String ): Int {
32
+ val result = mutableListOf<String >()
33
+ val maxLength = s.length
34
+ var startIndex = 0
35
+ while (startIndex < maxLength) {
36
+ val endIndex = startIndex + 1
37
+ for (i in endIndex until maxLength + 1 ) {
38
+ val temp = s.substring(startIndex, i)
39
+ if (s.contains(temp.reversed())) {
40
+ result.add(temp)
41
+ }
42
+ }
43
+ startIndex + = 1
44
+ }
45
+ return result.size
46
+ }
You canโt perform that action at this time.
0 commit comments