Skip to content
23 changes: 23 additions & 0 deletions contains-duplicate/dusunax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'''
# Leetcode 217. Contains Duplicate

use set to store distinct elements 🗂️

## Time and Space Complexity

```
TC: O(n)
SC: O(n)
```

### TC is O(n):
- iterating through the list just once to convert it to a set.

### SC is O(n):
- creating a set to store the distinct elements of the list.
'''

class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return len(nums) != len(set(nums))

28 changes: 28 additions & 0 deletions valid-palindrome/dusunax.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

파이썬 라이브러리와 문법은 정말.. 알고리즘 풀기에 행복한 수준이네요 ㅎㅎ

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅎㅎㅎㅎ 공감합니다

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'''
# Leetcode 125. Valid Palindrome

use regex to filter out non-alphanumeric characters 🔍

## Time and Space Complexity

```
TC: O(n)
SC: O(n)
```

### TC is O(n):
- iterating through the string just once to filter out non-alphanumeric characters.

### SC is O(n):
- creating a new string to store the filtered characters.
'''

class Solution:
def isPalindrome(self, s: str) -> bool:
if s is " ":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python에서 is의 경우 짧은 문자열일 경우에는 같은 객체로 취급하기때문에 equal이지만 긴 문자열일때는 ==를 사용해야 한다고 하네요. 몰랐던 사실이라 공유해봅니다!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다~! 아직 python에 익숙하지 않아서 실수했습니다

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

지금도 사실 짧은 문자열이라 사실 결과값은 같으니까 문제 없는 코드라고 생각됩니다!

return True

reg = "[^a-z0-9]"
converted_s = re.sub(reg, "", s.lower())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regex가 간편해서 저도 사용하긴 했는데 사실 regex보단 내장함수들이 성능이 좋을거라 gpt한테 물어봤더니 이런 코드를 제안해주네요! (코드가 그렇게 가독성이 좋진 않은 것 같아서 좋은진 잘 모르겠습니다... ㅎ..)

class Solution:
    def isPalindrome(self, s: str) -> bool:
        # 문자열을 소문자로 변환하고 알파벳과 숫자만 남김
        converted_s = ''.join(c for c in s.lower() if c.isalnum())
        # 회문 여부 판단
        return converted_s == converted_s[::-1]

Copy link
Member Author

@dusunax dusunax Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코멘트 잘 봤어요!

말씀하신 대로 내장함수 기반 접근이 regex보다 성능상 유리할 것 같습니다😀

그리고 제너레이터 표현식을 사용하는 것도 효율성 면에서 좋은 것 같습니다.
약간의 가독성을 향상을 위해서 s.lower만 분리해서 적용해봤어요

s = s.lower()
converted_s = ''.join(c for c in s if c.isalnum())


return converted_s == converted_s[::-1]
Loading