Medium

Given a string, find out if its characters can be rearranged to form a palindrome.

Example

For inputString = "aabb", the output should be
palindromeRearranging(inputString) = true.

We can rearrange "aabb" to make "abba", which is a palindrome.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    A string consisting of lowercase English letters.

    Guaranteed constraints:
    1 ≤ inputString.length ≤ 50.

  • [output] boolean

    • true if the characters of the inputString can be rearranged to form a palindrome, false otherwise.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

bool palindromeRearranging(char *inputString)
{
	int alphabet[26]={0, };
	bool isOdd=false;

	for(int i=0;i<strlen(inputString);i++)
		alphabet[inputString[i]-'a']++;

	for(int i=0;i<26;i++)
		if(alphabet[i]%2==1)
		{
			if(isOdd)
				return false;
			else
				isOdd=true;
		}

	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> arrayMaximalAdjacentDifference  (0) 2020.04.05
<Codesignal> areEquallyStrong  (0) 2020.04.05
<Codesignal> arrayChange  (0) 2020.04.05
<Codesignal> Are Similar?  (0) 2020.04.05
<Codesignal> Add Border  (0) 2020.04.05

+ Recent posts