File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ package leetcode_study
2
+
3
+ /* *
4
+ * ๋ฌธ์์ด์ ๋์นญ ํ๋จ
5
+ * ์๊ฐ ๋ณต์ก๋ : O(n)
6
+ * -> ์ฃผ์ด์ง(n) ํฌ๊ธฐ์ ๋ฌธ์์ด์ ์ํํ๋ฉฐ ๋น๊ตํจ
7
+ */
8
+ fun isPalindrome (s : String ): Boolean {
9
+
10
+ // ์ฃผ์ด์ง ๋ฌธ์์ด์์ ๋ชจ๋ non-alphanumeric characters๋ฅผ ํํฐํ ๋ฌธ์ ๋ฐฐ์ด ํ ๋น
11
+ val splitGivenString = s.toCharArray()
12
+ .filter { it.isLetter() || it.isDigit() }
13
+ .map { it.lowercaseChar() }
14
+ .toCharArray()
15
+
16
+ // ํํฐ๋ ๋ฌธ์์ด์ด ๋น์ด์๋ค๋ฉด true ๋ฐํ
17
+ if (splitGivenString.isEmpty()) { return true }
18
+
19
+ // ๋์นญ์ ๋น๊ตํ๊ธฐ ์ํ ์์, ๋ ๋ณ์
20
+ var start = 0
21
+ var end = splitGivenString.size - 1
22
+
23
+ // ๋ฐ๋ณต์ ์ผ๋ก ์ํํ๋ฉฐ ๊ฐ๋ค๋ฉด ์์ +1, ๋ -1 ์ ์งํํด ๋์นญ ํ๋จ
24
+ while (start <= end) {
25
+ if (splitGivenString[start] == splitGivenString[end]) {
26
+ start + = 1
27
+ end - = 1
28
+ continue
29
+ }
30
+ return false
31
+ }
32
+ return true
33
+ }
You canโt perform that action at this time.
0 commit comments