Results 1 to 4 of 4
Thread: using arrays
- 04-17-2010, 08:23 AM #1
Member
- 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!
- 04-17-2010, 10:06 AM #3
Member
- 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; 04-17-2010 at 11:19 AM.
- 04-17-2010, 11:47 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
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:
The method above finds the length of the longest common prefix of two Strings s1 and s2; the prefix has at most n characters. This little method is the heart of your program; I did the hard part; all you have to do is apply this method for all your Strings in an array. Oh, and you have to read those Strings from somewhere first.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 i-1; // that last char didn't match so i-1 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: 03-23-2010, 01:40 AM -
Arrays
By karabo101 in forum New To JavaReplies: 12Last Post: 10-11-2009, 05:02 PM -
Need help with 2D arrays...
By rrsv2 in forum New To JavaReplies: 3Last Post: 11-30-2008, 03:15 AM -
2D Arrays
By Major90 in forum New To JavaReplies: 6Last Post: 11-06-2008, 02:08 PM -
Need help with Arrays
By dietgal in forum New To JavaReplies: 21Last Post: 10-08-2008, 01:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks