Results 1 to 4 of 4
Thread: using arrays
 04172010, 09:23 AM #1Member
 Join Date
 Apr 2010
 Posts
 2
 Rep Power
 0

So what have you got so far? Please use code tags when posting code (see my signature for details on how to do this). Best of luck!
 04172010, 11:06 AM #3Member
 Join Date
 Apr 2010
 Posts
 2
 Rep Power
 0
i dont know how to start just i know the logic that by using array we can store strings and then by using for loop we will compare each letter of first string with second and so on. plz tell me the code..
Last edited by monikat; 04172010 at 12:19 PM.
 04172010, 12:47 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,765
 Blog Entries
 7
 Rep Power
 21
Analyze this little problem to death first and only then go from the logic to the Java implementations; I start: you have a set (or array) of Strings: pS1, pS2, ... pSn where p is the longest common prefix; the task is to find p.
For the first two Strings pS1 and pS2 you don't know any prefix yet so it is safe to assume that the lenth of the prefix is at most pS1 == n. The job reduces to finding a common longest prefix of two strings Si and Sj of at most n characters. The common prefix has at most n characters. Also, n can not be larger than Si or Sj.
Here is where Java kicks in:
Java Code:public int prefixLength(String si, String sj, int n) { n= Math.min(Math.min(n, si.length()), sj.length()); // find a posible valid n // find the length of the largest prefix of si and sj for (int i= 0; i <= n; i++) if (!sj.startsWith(si.substring(0, i)) return i1; // that last char didn't match so i1 return n; // all n characters matched }
kind regards,
Jos
Similar Threads

Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03232010, 02:40 AM 
Arrays
By karabo101 in forum New To JavaReplies: 12Last Post: 10112009, 06:02 PM 
Need help with 2D arrays...
By rrsv2 in forum New To JavaReplies: 3Last Post: 11302008, 04:15 AM 
2D Arrays
By Major90 in forum New To JavaReplies: 6Last Post: 11062008, 03:08 PM 
Need help with Arrays
By dietgal in forum New To JavaReplies: 21Last Post: 10082008, 02:59 PM
Bookmarks