Sunday, April 28, 2024
HomeJava Tips on how to discover the Longest widespread prefix in Array...

[Solved] Tips on how to discover the Longest widespread prefix in Array of String in Java? Instance Tutorial



Initially, allow us to perceive what the issue is and what it has
for us to know.
One of the best recommendation I may give you guys is that earlier than
making an attempt to leap into writing the code, perceive the query totally
and attempt to make up in thoughts the answer. If you’re dealing with difficulties,
seize a pen and paper and attempt to jot it down. so, what is the wait? let’s
begin!

What’s the Longest Widespread Prefix in an array of String?

Contemplate we now have ‘N’ strings with us. We have to discover the longest widespread
prefix of these n strings.

The longest widespread prefix is outlined because the longest
substring
which is widespread to all of the strings and that too is a prefix. A little bit of
confusion? don’t fret. allow us to break down the phrases and perceive what
every one means.

A prefix is a set from the start. So, taking an instance, to illustrate
there’s a string s = “Longest”

Now, it is prefix may be {“L”, “Lo”, “Lon”, “Lengthy”, “Longe”, “Longes”,
“Longest”}. As seen, any measurement of substring from beginning is a
prefix. 

Now, in our downside, we have to
discover the longest prefix that’s widespread to all of the strings.

Bought it?

Let’s perceive extra with the assistance of an instance. As an instance we now have 6
strings as talked about under:

  • FastAndFurious
  • FastAndFuriousAgain
  • FastAndVeryFurious
  • FastButNotFurious
  • FasterAndFurious
  • FastestAndReckless

Now, get paper and pen and take a look at it out your self. Discover the longest
widespread prefix. take 2 minutes and clear up it.

How to find the Longest common prefix in given Array of String in Java? [Solved]

Now, for individuals who have solved. What’s the reply?
What’s the longest widespread prefix amongst these strings? Sure!
the reply is “Quick”. This substring is a prefix to all strings and is widespread and longest.

So, how did you do it? you could have an method and we are going to see it
later. For individuals who could not determine it out. No worries, let’s talk about
it.

This query may be solved utilizing a number of methods. Explaining all of
these is out of scope for this text. However, we are going to attempt to perceive a
few of them that are able to fixing the query and have a good
complexity.

2. The Horizontal Scanning method to seek out longest widespread Prefix

For this, we are going to scan horizontally, which implies, we are going to first discover the
longest widespread prefix of two strings, then 3 strings, happening until ‘N’.
sounds easy proper? It’s! allow us to test this by writing a code and
checking how the implementation works.

Coding Answer of Longest widespread prefix in a set of String

import java.util.Scanner;

public class LongestCommonPrefix {

public static void predominant(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the dimensions of array:");
int n = sc.nextInt();
System.out.println("Enter the weather of array:");
String[] array = new String[n];
sc.nextLine();
//remember to not enter any area!
for(int i=0;i<n;i++) {
array[i] = sc.nextLine();
}
LongestCommonPrefix lcp = new LongestCommonPrefix();
String longestCommonPrefix = lcp.findLongestCommonPrefix(array);
System.out.print("the longest widespread prefix from these strings is: " +longestCommonPrefix);

}

public String findLongestCommonPrefix(String[] strs) {
if (strs.size == 0) return "";
String prefix = strs[0];
for (int i = 1; i < strs.size; i++)
whereas (strs[i].indexOf(prefix) != 0) {
//System.out.println(strs[i]);
//System.out.println(prefix);
//System.out.println(strs[i].indexOf(prefix));
prefix = prefix.substring(0, prefix.size() - 1);
if (prefix.isEmpty()) return "";
}
return prefix;
}
}



Output:

After observing the code implementation and the output fastidiously, we will
see how this works. Let me information you on how this works. First, we hold
the prefix as “FastAndFurious”, which is, our first String within the array.

Now, we begin a traversal from string 2 and test and replace the prefix
that’s widespread to string 1 and a couple of, which is “FastAndFurious”. we go forward
on the third string, the place we get “Quick” and so forth.

After finishing the ultimate loop, we arrive at a state the place all of the
strings are processed and we now have our widespread prefix that’s the longest!
Tada! you guys can test the output and confirm this. Additionally, be at liberty to
carry out a hands-on for a similar.

Time complexity: O(Ok), the place Ok = sum of all characters of all
strings mixed.

Area complexity: O(1)

Now, let’s rapidly transfer on to a different methodology. 

3. The Vertical Scanning method to seek out Longest Widespread Prefix in an
Array of String in Java

On this method, relatively than checking for every string, we are going to work
vertically, that’s, we are going to test all of the strings without delay and get the
longest widespread prefix. let’s have a look at the code for this too and the way it
works.

import java.util.Scanner;

public class LongestCommonPrefix {

public static void predominant(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the dimensions of array:");
int n = sc.nextInt();
System.out.println("Enter the weather of array:");
String[] array = new String[n];
sc.nextLine();
//remember to not enter any area!
for(int i=0;i<n;i++) {
array[i] = sc.nextLine();
}
LongestCommonPrefix lcp = new LongestCommonPrefix();
String longestCommonPrefix = lcp.findLongestCommonPrefix(array);
System.out.print("the longest widespread prefix from these strings is: " +longestCommonPrefix);

}

public String findLongestCommonPrefix(String[] strs) {
if (strs == null || strs.size == 0) return "";
for (int i = 0; i < strs[0].size() ; i++){
char c = strs[0].charAt(i);
for (int j = 1; j < strs.size; j ++)
}
return strs[0];
}
}

Output:

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments