Question

In: Computer Science

Give an english description of an algorithm which can determine whether or not characters can be...

Give an english description of an algorithm which can determine whether or not characters can be arranged into a palindrome when given an input string of X characters. Then, determine order of growth of this algorithm as a function of X. (These characters can only be any of the twenty six lower case alphabet letters.)

Solutions

Expert Solution

search

Sign In

Home

Courses

Hire With Us

Algorithmskeyboard_arrow_down

Data Structureskeyboard_arrow_down

Languageskeyboard_arrow_down

Interview Cornerkeyboard_arrow_down

GATEkeyboard_arrow_down

CS Subjectskeyboard_arrow_down

Studentkeyboard_arrow_down

GBlog

Puzzles

What's New ?

Check if characters of a given string can be rearranged to form a palindrome

Last Updated: 03-03-2020

Given a string, Check if characters of the given string can be rearranged to form a palindrome.

For example characters of “geeksogeeks” can be rearranged to form a palindrome “geeksoskeeg”, but characters of “geeksforgeeks” cannot be rearranged to form a palindrome.

Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

A set of characters can form a palindrome if at most one character occurs odd number of times and all characters occur even number of times.

A simple solution is to run two loops, the outer loop picks all characters one by one, the inner loop counts number of occurrences of the picked character. We keep track of odd counts. Time complexity of this solution is O(n2).

We can do it in O(n) time using a count array. Following are detailed steps.

1) Create a count array of alphabet size which is typically 256. Initialize all values of count array as 0.

2) Traverse the given string and increment count of every character.

3) Traverse the count array and if the count array has more than one odd values, return false. Otherwise return true.

Another approach:

We can do it in O(n) time using a list. Following are detailed steps.

1) Create a character list.

2) Traverse the given string.

3) For every character in the string, remove the character if the list already contains else add to the list.

3) If the string length is even the list is expected to be empty.

4) Or if the string length is odd the list size is expected to be 1

5) On the above two conditions (3) or (4) return true else return false.

Hope this may help you

Thank you ??


Related Solutions

Give a valence bond theory description of the bonding in PF3. Also determine whether PF3 is...
Give a valence bond theory description of the bonding in PF3. Also determine whether PF3 is polar . (a)What is the Lewis structure for PF3? (b)Make a 3-D drawing for PF3. What are the geometry and approximate bond angles for PF3? (c)What is the condensed orbital-filling diagram for the central atom? (d)What is the condensed orbital-filling diagram for the terminal atoms? (e)Which half-filled orbitals of the central atom overlap with which half-filled orbitals of the terminal atoms to form the...
Exercise 1.13.5: Determine and prove whether an argument in English is valid or invalid. Prove whether...
Exercise 1.13.5: Determine and prove whether an argument in English is valid or invalid. Prove whether each argument is valid or invalid. First find the form of the argument by defining predicates and expressing the hypotheses and the conclusion using the predicates. If the argument is valid, then use the rules of inference to prove that the form is valid. If the argument is invalid, give values for the predicates you defined for a small domain that demonstrate the argument...
Design an algorithm that will read an array of 200 characters and display to the screen...
Design an algorithm that will read an array of 200 characters and display to the screen a count of the occurrences of each of the five vowels (a, e, i, o, u) in the array. Your string is: The quick brown fox jumped over the lazy dog That is the question for my pseudo code question, I don't know if I have this correct can i have someone look this over and finish this and explain what i needed to...
Give an algorithm to detect whether a given undirected graph contains a cycle. If the graph...
Give an algorithm to detect whether a given undirected graph contains a cycle. If the graph contains a cycle, then your algorithm should output one. (It should not output all cycles in the graph, just one of them.) The running time of your algorithm should be O(m + n) for a graph with n nodes and m edges.
Question 1: Determine whether the given description to an observational study or an experiment. In a...
Question 1: Determine whether the given description to an observational study or an experiment. In a study of 424 children with particular disease, the subjects were monitored with an EEG while asleep. Does the given description correspond to an observational study or an experiment? A. The given description corresponds to an experiment. B. The given description corresponds to an observational study. C. The given description does not provide enough information to answer this question. Question 2: Identify the type of...
Write a function which lets the user enter English words. The user can keep entering English...
Write a function which lets the user enter English words. The user can keep entering English words as long as the user has not entered the word “exit”. Once the user enters “exit” the function will return and print the list of all distinct words starts with English alphabets. Like: A: Ali, apple, … B: Bob, book … until Z. Write a python program for this question. Use main function.
5. Determine whether the given description corresponds to an experiment or observational study. a. A stock...
5. Determine whether the given description corresponds to an experiment or observational study. a. A stock analyst selects a stock from a group of twenty for investment by choosing the stock with the greatest earnings per share reported for the last quarter. b. A marketing firm does a survey to find out how many people use a product. Of the one hundred people contacted, fifteen said they use the product. c. A political pollster reports that his candidate has a...
Python English algorithm explanation Write a program that asks the user for the name of a...
Python English algorithm explanation Write a program that asks the user for the name of a file in the current directory. Then, open the file and process the content of the file. 1)If the file contains words that appear more than once, print “We found duplicates.” 2)If the file does not contain duplicate words, print “There are no duplicates.”
Python English algorithm explanation Write a program that asks the user for the name of a...
Python English algorithm explanation Write a program that asks the user for the name of a file in the current directory. Then, open the file and process the content of the file. 1)If the file contains words that appear more than once, print “We found duplicates.” 2)If the file does not contain duplicate words, print “There are no duplicates.”
Determine whether or not the following languages are regular. If the language is regular then give...
Determine whether or not the following languages are regular. If the language is regular then give an NFA or regular expression for the language. Otherwise, use the pumping lemma for regular languages or closure properties to prove the language is not regular. 1) L = { 0 n1 k : k ≤ n ≤ 2k} 2) L = { 0 n1 k : n > 0, k > 0 } È { 1 k0 n : k > 0, n...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT