![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
|
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 .
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#2 |
|
Professional Programmer
|
And another thing : in this quick sort I get:
Exception in thread "main" java.lang.StackOverflowError import java.util.*;
class quicksort{
public static void main(String args[]){
final int N=1000;
final int L=1000;
int a[]=new int[N];
Random r=new Random();
for(int i=0;i<N;i++)
a[i]=r.nextInt(L);
Date d=new Date();
long t0=d.getTime();
quickSort(a,0,N-1);
d=new Date();
long t1=d.getTime();
System.out.println("Sorting time="+((t1-t0)/1000)+"sec");
}
public static void quickSort(int a[],int left, int right){
int p=Pivot(a,left,right);
quickSort(a,left,p-1);
quickSort(a,p+1,right);
}
public static int Pivot(int a[],int left,int right){
int i,j;
i=left;
j=right;
int di=0;
int dj=1;
while(i<j){
if(a[i]>a[j]){
int aux=a[i];
a[i]=a[j];
a[j]=aux;
//aux=di;
aux=di;
di=dj;
dj=aux;
}
i=i+di;
j=j-dj;
}
return i;
}
}HELP please!!
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2004
Location: UK
Posts: 3
Rep Power: 0
![]() |
Couldnt you just compare the strings somehow? instead of using arrays? (Not sure if that's possible, I'm new to Java too)
__________________
It's nice to be nobody. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Nov 2004
Posts: 21
Rep Power: 0
![]() |
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.
__________________
<span style='font-family:Courier'><span style='font-size:12pt;line-height:100%'><span style='color:orange'>☼☼☼ Just kill em all, and let God sort em out. ☼☼☼</span></span></span> |
|
|
|
|
|
#5 |
|
Professional Programmer
|
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[]){
int i;
int d=a.length;
for(i=0;i<a.length-1;i++){
if(a[i]==a[i+1]){
a[i]=a[i+1];
d--;
}
}
}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.......... <_<
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#6 |
|
Programming Guru
![]() |
Have you considered bubble sort?
public static void bubbleSort(int a[]){
int len = a.length();
for(int i=0;i<len - 1;i++)
for(int j=0;j<len - 1 - i;j++)
if(a[j] > a[j+1]) {
int tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
__________________
|
|
|
|
|
|
#7 | |
|
Professional Programmer
|
First ... i think your sorting doesn't work... the second for should be :
for(int j=0;j<len;j++) Quote:
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Nov 2004
Posts: 16
Rep Power: 0
![]() |
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 )
{
Vector str1 = array1.toVector();
Vector str2 = array2.toVector();
Vector intersection = new Vector();
for ( Iterator i = new str1.iterator(); i.hasNext(); )
{
if ( str2.contains( i.next() ) && !( intersection.contains(i.next()) ) )
{
intersection.add( i.next() );
}
}
char[] intersect = new char[intersection.size()];
for ( int i=0; i < intersection.size(); i++ )
{
intersect[i] = (char)intersection.get(i);
}
return intersect;
}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. |
|
|
|
|
|
#9 |
|
Professional Programmer
|
I did it in the end ... lilke this :
class comun{
public static void main(String args[]){
final int N=100;
String s1="abccd";
String s2="abbdccga";
char c1[];
char c2[];
c1=s1.toCharArray();
c2=s2.toCharArray();
int i;int j;
char test[]=new char[N];
char test1[]=new char[N];
int k=0;
int p=0;
for(i=0;i<c1.length;i++){
boolean esteComun=false;
for(j=0;j<c2.length;j++){
if(c1[i]==c2[j]){
esteComun=true;
break;
}
}
if(esteComun==true){
test[k]=c1[i];
k++;
}
}
char w[] = new char[k];
System.arraycopy(test, 0, w, 0, k);
for(i=0;i<c2.length;i++){
boolean esteComun=false;
for(j=0;j<c1.length;j++){
if(c2[i]==c1[j]){
esteComun=true;
break;
}
}
if(esteComun==true){
test1[p]=c2[i];
p++;
}
}
char q[] = new char[p];
System.arraycopy(test1, 0, q, 0, p);
char x[] = new char[k+p];
for(i=0;i<w.length;i++)x[i]=w[i];
for(i=0;i<q.length;i++)x[i+w.length]=q[i];
sort (x);
// for(i=0;i<x.length;i++)System.out.print(x[i]);
int cont=1;
int max=0;
char val=0;
for (i=0;i<x.length-1;i++){
if(x[i]==x[i+1]){
cont++;
if(cont>max){max=cont;
val=x[i];}
}
else cont=1;
}
System.out.print(val);
}
private static void sort(char a[]){
int i;
for(i=0;i<a.length-1;i++)
for(int j=i;j<a.length;j++)
if(a[i]>a[j]){char aux=a[i];
a[i]=a[j];
a[j]=aux;
}
}
}I think i did the same thig twice in the midle there ... but u get the picture... it works :banana: 10x for your help
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|