Welcome to Dream.In.Code
Become a Java Expert!

Join 149,519 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,390 people online right now. Registration is fast and FREE... Join Now!




foming palindrome from given string

 
Reply to this topicStart new topic

foming palindrome from given string, from the characters of th e given string

pavani2006
22 May, 2007 - 06:00 AM
Post #1

New D.I.C Head
*

Joined: 22 Feb, 2007
Posts: 15



Thanked: 2 times
My Contributions
i want to check whether the palindrome can be formed from the characters of the given string
suppose the givenword is "opap"
it has to display that pop palindrome from the given word
if the input is "opar" it should display that palindrome cant be formed from the given string.
and my logic
string str,str1;int k=0;
for(i=0;i<str.length();i++)
{ for(j=i+1;j<str.length();j++)
{
int j=str.length()-1;
if(str.charAt(i0=str.charAt(j))
k++;
}
if(k>1)
{
str1.charAt(0)=str.charAt(i);
str1.charat(2)=str.charAt(i);
str1.charAt(1)=str.charAt(0);
}
}
}
i know only this but i want the corrrect

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Foming Palindrome From Given String
22 May, 2007 - 08:24 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
there are 2 approaches, 1 would be to gnerate every possible combination of letters, and write an isPalindrome(String s) method, this is very expensive and not efficient in the slightest, so i would suggest you try the following:
check the word for each letter contained in the word and get a count of the number of times that letter appears, if the letter has a count greater than 2, then the letter can be used in a palindrome, along with 1 letter which has a count of 1. The method which constructs the palindrome would simply place each even letter then the odd letter, and reverse the order of the even letters. This also depends on if you are using a database of real words which must exist, or if the fact that it is a palidrome is enough.

Hopefully this is enough to get you going, this will also work for any length of word. Using your examples:
opap:
o = 1
p = 2
a = 1
so your output could be: pap, or pop

another: qwerherwnw
q = 1
w = 3
e = e
r = 2
h = 1
j = 1
n = 1
the output could be: wreherw, rewnwer, erwwwre, etc
User is offlineProfile CardPM
+Quote Post

pavani2006
RE: Foming Palindrome From Given String
23 May, 2007 - 08:58 PM
Post #3

New D.I.C Head
*

Joined: 22 Feb, 2007
Posts: 15



Thanked: 2 times
My Contributions
please can u give the code for the method how to arrange that even character and odd character to form palindrome.please
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:17PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month