![]() |
Hi, my problem is as folows: I have 2 strings : exapmple: s1="aabbccdef" and s2="abcddefd" . Now i have to find out wich letter is found more times in the 2 strings and is common to both. I turned them into char .. with toCharArray() but i'm having problems with comparing the 2. In this case the aswere should be "d".
The first thing would be to take all the commone letters and put them in .... char c3=new char[ ? ] . I'm not sure how long it should be. from then it should be simple. Thanks for the help i hope u'll give me . |
And another thing : in this quick sort I get:
:
Exception in thread "main" java.lang.StackOverflowError:
import java.util.*;HELP please!! |
Couldnt you just compare the strings somehow? instead of using arrays? (Not sure if that's possible, I'm new to Java too)
|
just an idea ur comparing letters right...well u can put the alphabet in an array of 26 and using a for loop u can make it take each letter in ur string and compare it to the next using a nested second for loop.
Then create urself a c3 empty string and make some ifs and elses that will replace the char stored everytime it finds a char with a higher index. Set your for loop to do this 26 times. That method should work fine since it covers any range of Strings u might input. |
the problem is that i can't get all the common ellements into another char[]
Example: c1:abbc =========>c3:aaabbbbcc this is what i'm having problems getting. c2:bbbcdaa even if i sort first the 2 strings : c1:abbc c2:aabbbcd I can't just comare the 2 and put them in c3. For example the first 'a' would be put in twice !!!! I thaught about reducing the strings: s1:abc s2:abcd Then compare c1 with s2 and c2 with s1 --> then put what's commone from each in c3 ... after that it would be simple. :
private static int removeExtra(char a[]){that's how i'm thinking of doing it. but i have to return th letters that remain .. and not sure how to do it...... and what to write in main. Hmmmmmm.......... <_< |
Have you considered bubble sort?
:
public static void bubbleSort(int a[]){ |
First ... i think your sorting doesn't work... the second for should be :
:
for(int j=0;j<len;j++)Quote:
|
I realise its a bit ugly - but perhaps something like this might help??
then you can just call it with your two character arrays and it will return an array representing the (unique) intersection. then you can iterate over that, and you get the bonus that you know how many chars are actually common to each, so you might use that to initialize an array to store the number of occurences in or whatever... I haven't tried to compile this but if it does have problems you should be able to fix them. :
public char[] intersection( char[] array1, char[] array2 )P.S. I didn't use the toArray() method because we want a char array, not an object array. which reminds me - the use of the toVector() method may not work for the same reason. it's all very inelegant really. |
I did it in the end ... lilke this :
:
class comun{I think i did the same thig twice in the midle there ... but u get the picture... it works :D :banana: 10x for your help |
| All times are GMT -5. The time now is 1:08 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC