CodeSignal-palindromeRearranging-Python

cosmos·2023년 4월 24일
0
post-thumbnail

문제

코드

def solution(inputString: str) -> bool:
    dict = {}
    one_coin = 1
    
    for x in inputString:
        if x not in dict:
            dict[x] = 1
        else:
            dict[x] += 1
    
    
    for key, value in dict.items():
        if value % 2 == 1:
            one_coin -= 1
        if one_coin < 0:
            return False
            
    return True

결과

ChatGPT Code

def solution(inputString):
    # Step 1: Create a dictionary to store the frequency of each character in the input string.
    freq = {}
    for ch in inputString:
        if ch in freq:
            freq[ch] += 1
        else:
            freq[ch] = 1

    # Step 2: Iterate through the dictionary to count the number of characters with odd frequency.
    odd_count = 0
    for count in freq.values():
        if count % 2 == 1:
            odd_count += 1

    # Step 3: If the count of characters with odd frequency is greater than 1, return False.
    if odd_count > 1:
        return False

    # Step 4: Otherwise, return True.
    return True

ChatGPT 문제풀이

주어진 문자열의 문자들을 재배열하여 회문(palindrome)을 만들 수 있는지 여부를 판단하는 문제입니다.
예를 들어, 문자열 "aabb"가 주어졌을 때, "abba"와 같이 문자들을 재배열하면 회문을 만들 수 있습니다. 따라서 이 경우에는 True를 반환해야 합니다.
문자열은 소문자 알파벳으로만 이루어져 있으며, 문자열의 길이는 1 이상 50 이하입니다.

문제출처 & 깃허브

CodeSignal
Github

0개의 댓글