Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

48-xxubin04 #182

Merged
merged 2 commits into from
Jun 12, 2024
Merged

48-xxubin04 #182

merged 2 commits into from
Jun 12, 2024

Conversation

xxubin04
Copy link
Member

@xxubin04 xxubin04 commented May 27, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

LeetCode 125. Valid Palindrome


โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30๋ถ„


โœจ ์ˆ˜๋„ ์ฝ”๋“œ

1. ๋ฌธ์ œ ์ดํ•ด

์ฃผ์–ด์ง€๋Š” ๋ฌธ์žฅ์—์„œ ์˜์–ด์™€ ์ˆซ์ž ์™ธ์˜ ์š”์†Œ๋ฅผ ์ œ์™ธํ•œ ๋‹จ์–ด๊ฐ€ ํŒฐ๋ฆฐ๋“œ๋กฌ์„ ์„ฑ๋ฆฝํ•˜๋ฉด true, ์„ฑ๋ฆฝํ•˜์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

# TestCase 1

Input: s = "A man, a plan, a canal: Panama"
Palindrome Check: "amanaplanacanalpanama"
Output: true

# TestCase 2

Input: 0p
Palindrome Check: "0p", "p0"
Output: false

์ฃผ์˜ํ•ด์•ผํ•  ์ 
submitํ•˜๊ณ  ๋ฐ˜๋ก€๋กœ 0p๊ฐ€ ๋‚˜์™€์„œ ๊ธ‰ํžˆ ์กฐ๊ฑด์„ .alpha()์—์„œ .alnum()์œผ๋กœ ์ˆ˜์ •ํ–ˆ๋‹ค.
์ฆ‰, ํ…Œ์ผ€์—์„œ๋Š” ๋‚˜์˜ค์ง€ ์•Š์•˜์ง€๋งŒ ๋ฐ˜๋ก€๋กœ ๊ฑธ๋ฆฌ๋Š” ๊ฒƒ์„ ๋ณด์•„ ์˜์–ด์™€ ์ˆซ์ž๋Š” ํŒฐ๋ฆฐ๋“œ๋กฌ ๊ฒ€์‚ฌ์— ํฌํ•จ์‹œ์ผœ์•ผ ํ•œ๋‹ค.


2. ์ฝ”๋“œ ๋ถ„์„

from collections import deque

class Solution(object):
    def isPalindrome(self, s):
        ans = deque()
        s_list = list(s)
        for i in s_list:
            if i.isalnum():
                ans.append(i.lower())
        if ''.join(ans) ==  ''.join(reversed(ans)):
            return True
        return False

๊ต‰์žฅํžˆ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€๋ฆฐ๋‹ค. ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž์—ด s๋ฅผ ๋ฆฌ์ŠคํŠธํ™”ํ•˜์—ฌ ํ•œ ๊ธ€์ž์”ฉ ๋ถ„๋ฆฌํ•œ s_list๋ฅผ for๋ฌธ์„ ํ†ตํ•ด ์ˆœํšŒํ•œ๋‹ค.
์ด ๊ณผ์ •์—์„œ .alnum() ์กฐ๊ฑด์œผ๋กœ, ์•ŒํŒŒ๋ฒณ์ด๊ฑฐ๋‚˜ ์ˆซ์ž์ธ ๊ฒฝ์šฐ์—๋Š” ans์— ์†Œ๋ฌธ์ž๋กœ ๋’ค์— ๋ถ™์ธ๋‹ค.
์•ŒํŒŒ๋ฒณ๊ณผ ์ˆซ์ž๋งŒ ์ฃผ๋ฃจ๋ฃฉ ๋ถ™์–ด์žˆ๋Š” ๋ฌธ์ž์ธ ans์™€ ์—ญ์ˆœ์œผ๋กœ ๋ถ™์ธ ans๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” True๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ทธ ์™ธ์—๋Š” False๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ด๋•Œ ์กฐ๊ธˆ ํ—ค๋งธ๋˜ ๊ฒŒ, ๋ฌธ์ œ์—์„œ๋Š” true, false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๊ณ  ํ•ด์„œ ๋ฌธ์ž์—ด๋กœ "true", "false"๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ ์•„๋ฌด๋ฆฌ ๊ณ ์ณ๋„ ํ…Œ์ผ€๊ฐ€ ํ†ต๊ณผ๋˜์ง€ ์•Š๋”๋ผ....
๋ฌธ์ œ์—์„œ ์†Œ๋ฌธ์ž๋กœ ๋ฐ˜ํ™˜ํ•ด๋„ ์ค๋Œ€์žˆ๊ฒŒ boolean ํ˜•์‹์ธ True, False๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ํ†ต๊ณผํ•œ๋‹ค.๐Ÿ‘๐Ÿป


์ฐธ๊ณ  ์ฝ”๋“œ
class Solution(object):
    def isPalindrome(self, s):
        s_list = list(s)
        word = ""
        reversed_word = ""
        for i in range(len(s_list)):
            if s_list[i].isupper():
                word += s_list[i].lower()
            elif s_list[i].isalpha() or s_list[i].isdigit():
                word += s_list[i]
        for j in reversed(list(word)):
            reversed_word += j
        if word == reversed_word:
            return True
        return False  

์ฒ˜์Œ ์ง  ์ฝ”๋“œ์—ฌ์„œ ๋งŽ์ด ์ง€์ €๋ถ„ํ•˜์ง€๋งŒ ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค. ใ…Žใ…Ž

image

์œ„์˜ ์ฝ”๋“œ๊ฐ€ ์™ผ์ชฝ, ์ฝ”๋“œ ๋ถ„์„ํ•œ ์ฝ”๋“œ๊ฐ€ ์˜ค๋ฅธ์ชฝ์ธ๋ฐ ์ฐจ์ด ๋งŽ์ด ๋‚˜๋„ค์š”..๐Ÿค”

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์—†์Šต๋‹ˆ๋‹ค~

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

ํŒŒ์ด์ฌ์€ ํŒฐ๋ฆฐ๋“œ๋กฌ ํ’€๊ธฐ ์ฐธ ์ข‹์€ ์–ธ์–ด๋ž€ ๋ง์ด์ฃ ...

class Solution:
    def isPalindrome(self, s: str) -> bool:
        s = ''.join(c.lower() for c in s if c.isalnum())
        return s == s[::-1]

์‚ฌ์‹ค, ํˆฌํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•œ ๋ฒˆ ์ˆœํšŒํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋„ ๋ฌธ์ž์—ด ํ•„ํ„ฐ๋ง + ํŒฐ๋ฆฐ๋“œ๋กฌ ๊ฒ€์‚ฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ต๋‹ˆ๋‹ค :)

class Solution:
    def isPalindrome(self, s: str) -> bool:
        lo, hi = 0, len(s) - 1
        while lo < hi:
            if not s[lo].isalnum():  # ํ•„ํ„ฐ๋ง
                lo += 1
                continue
                
            if not s[hi].isalnum():  # ํ•„ํ„ฐ๋ง
                hi -= 1
                continue
                
            if s[lo].lower() != s[hi].lower():  # ํŒฐ๋ฆฐ๋“œ๋กฌ ๊ฒ€์‚ฌ
                return False
            
            lo += 1
            hi -= 1
        return True

image
์œ„์—๊ฐ€ ํˆฌํฌ์ธํ„ฐ, ์•„๋ž˜๊ฐ€ ๋’ค์ง‘๊ธฐ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•œ ํ’€์ด์ž…๋‹ˆ๋‹ค.
์ข€ ์ฐจ์ด๋‚˜์ฃ ? :)

Comment on lines +5 to +9
ans = deque()
s_list = list(s)
for i in s_list:
if i.isalnum():
ans.append(i.lower())
Copy link
Collaborator

Choose a reason for hiding this comment

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

๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜๊ณผ ๋ฌธ์ž์—ด ์กฐ์ธ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•œ๋‹ค๋ฉด ๋ฌธ์ž์—ด s์˜ ํ•„ํ„ฐ๋ง์ด ๋งค์šฐ ๊ฐ„ํŽธํ•ด์ง‘๋‹ˆ๋‹ค :)

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

Comment on lines +10 to +12
if ''.join(ans) == ''.join(reversed(ans)):
return True
return False
Copy link
Collaborator

Choose a reason for hiding this comment

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

ํŒŒ์ด์ฌ์˜ ๋ฆฌ์ŠคํŠธ ์ธ๋ฑ์‹ฑ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜๋ฉด, ๊ฑฐ๊พธ๋กœ ์ˆœํšŒํ•˜๋Š” ๊ฑธ ์•„์ฃผ ์‹ฌํ”Œํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค :)

reversed_str = filtered_str[::-1]  # [์ฒ˜์Œ๋ถ€ํ„ฐ:๋๊นŒ์ง€:์—ญ์ˆœ์œผ๋กœ]

๊ฒฐ๊ตญ, ์ตœ์ข…์ ์œผ๋กœ ํ•„ํ„ฐ๋ง๋œ ๋ฌธ์ž์—ด๊ณผ ํ•„ํ„ฐ๋ง๋œ ๋ฌธ์ž์—ด์„ ๋’ค์ง‘์€ ๊ฒŒ "๋™์ผํ•˜๋ƒ ์•ˆ๋™์ผํ•˜๋ƒ"์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ˆ, ๊ทธ ๋‘˜์„ ๋น„๊ตํ•œ ๊ฒฐ๊ณผ ์ž์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋ฉด ๋˜๊ฒ ์ฃ ?

return filtered_str == reversed_str

mjj111

This comment was marked as duplicate.

Copy link
Collaborator

@mjj111 mjj111 left a comment

Choose a reason for hiding this comment

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

์ €๋Š” ํ•ด๋‹น s์˜ ๊ธธ์ด๊ฐ€ ๊ธธ์ง€์•Š์•„์„œ, ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.

ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์ œ์™ธํ•œ sentence๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
์ดํ›„์— sentence์˜ ๊ธธ์ด์˜ ๋ฐ˜๋งŒํผ ๋ฐ˜๋ณต ์ ‘๊ทผํ•˜์—ฌ ์•ž ๋’ค ์ธ๋ฑ์Šค์— ๋ฌธ์ž๋ผ๋ฆฌ ๋น„๊ตํ–ˆ์Šต๋‹ˆ๋‹ค
ํŒฐ๋ฆฐ๋“œ๋กฌ ๋„ˆ๋ฌด ์˜ค๋žœ๋งŒ์— ํ’€์–ด์„œ ๋ฐ˜๊ฐ‘๋„ค์—ฌ ใ…Ž ๐Ÿ™‹โ€โ™‚๏ธ

import java.util.*;
class Solution {
    public boolean isPalindrome(String s) {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < s.length(); i++) {
            char now = s.charAt(i);
            if(Character.isAlphabetic(now)|| Character.isDigit(now)) {
                sb.append(now);
            }
        }

        String sentence = sb.toString().toLowerCase();
        int N = sentence.length();
        for(int i = 0; i < N / 2; i++) {
            char left = sentence.charAt(i);
            char right = sentence.charAt(N-i-1);
            if(left != right) return false;
        }
        return true;
    }
}

Copy link
Member

@gjsk132 gjsk132 left a comment

Choose a reason for hiding this comment

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

์ €๋Š” filter์‚ฌ์šฉํ•ด์„œ ๋ถˆํ•„์š”ํ•œ ๋ถ€๋ถ„์„ ์ œ๊ฑฐํ•˜๊ณ ,
๋ฌธ์ž์—ด ๊ธธ์ด์˜ ์ ˆ๋ฐ˜๋งŒ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!

class Solution:
    def isPalindrome(self, s: str) -> bool:
        s = [i.lower() for i in filter(str.isalnum, s)]
        n = len(s)

        for i in range(n//2):
            if s[i] != s[-i-1]:
                return False

        return True

๋‹ค์–‘ํ•œ ํ’€์ด๋ฒ•์„ ์•Œ ์ˆ˜ ์žˆ์–ด์„œ ์žฌ๋ฐŒ๋„ค์š” ๐Ÿ˜„๐Ÿ‘

@gjsk132
Copy link
Member

gjsk132 commented Jun 11, 2024

๊ตํ™ฉ์„ ๋ฐฐ ์ฝ”๋“œ ์ฐธ๊ณ ํ•ด์„œ, ์Šˆ์ƒค์ƒฅ ๋” ์งง์€ ์‹œ๊ฐ„์œผ๋กœ ๋งŒ๋“ค์–ด๋ฒ„๋ฆฌ๊ธฐ!!

image

class Solution:
    def isPalindrome(self, s: str) -> bool:
        s = [i.lower() for i in filter(str.isalnum, s)]
        n = len(s)
        return s[:n:1] == s[n::-1]

@xxubin04 xxubin04 merged commit af8b7f9 into AlgoLeadMe:main Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants