Java find all substrings in string

Find all possible substring in fastest way [duplicate]

But according to my understanding the complexity goes to O(N^2) . Can we make it faster? I referred previous question and there was link for suffix tree but it doesn’t seem to solve my problem. The output which I get from suffix tree is

You can’t possibly make it faster than O(n^2) to even list the starting and ending points of each possible substring, since there are O(n^2) such substrings! If you want to print out each substring in full (as you are currently doing), then the time complexity goes up to O(n^3) since it takes time proportional to the overall string length to print each substring.

You can only make it faster if you’re going to run a query over the set of substrings that doesn’t «touch» them all. Printing them touches all of them. If you wanted to ask, say, «what is the longest substring that appears at least twice» or «which substring of more than k characters occurs most frequently», then you can do so without enumerating all substrings (with a suffix tree).

the for (int j = i+1; j

@HojjatK — The code in the question is correct. The 2nd argument to substring is an exclusive index, and therefore j does have to go up to A.length() . inclusive. See the javadoc.

1 Answer 1

You can’t create O(N^2) strings in better than O(N^2) time. It is a mathematical impossibility. Even if creating a string took one instruction, that is still a O(N^2) computation.

Putting complexity aside, I don’t think your code can be improved upon in any significant way.

Optimizing this particular piece of code is a futile activity. Since you are writing the strings to standard output, the actual performance will be dominated by the overheads of writing the characters . and whatever the OS does with the output.

Читайте также:  Php server request domain

Источник

How to find all substrings of a string in Java

In this short article, we are going to showcase how to find all substrings of a string in Java.

First, we will start by showing how to do this using core Java methods. Then, we are going to highlight how to achieve the same thing using external libraries.

Java provides several methods and classes to print all possible substrings of a given string. So, let’s take a close at each approach.

Using String.substring() Method

substring() offers the easiest way to get substrings from an original string.

So, let’s illustrate how we can use it using a practical example:

 String str = "Hello"; ListString> substringList = new ArrayListString>(); for (int i = 0; i for (int j = i + 1; j > System.out.println(substringList); 

using substring method

As we can see, we used two loops. Since the substring() method has a time complexity of o(n), the above approach has a o(n^3) time complexity.

Using StringBuffer Class

Similarly, we can use the StringBuffer class to extract all the substrings. However, the time complexity will jump to O(n)2.

So, let’s exemplify the use of the StringBuffer class:

 String str = "foo"; int length = str.length(); char[] strChars = str.toCharArray(); ListString> substringList = new ArrayListString>(); int i = 0; while (i < length) < StringBuffer buildStr = new StringBuffer(); buildStr.append(strChars[i]); for (int j = i + 1; j if (j < length) < buildStr.append(strChars[j]); >> i++; > System.out.println(substringList); 

The program will produce the output:

We used the toCharArray() method to convert a string into a char[] array. Then, we used the StringBuffer class to build all the possible portions.

Alternatively, another solution would be using the StringBuilder class. The only difference is that StringBuilder is not thread-safe.

Using Apache Commons Lang Library

This external library comes with a set of ready-to-use utility classes. Among these classes, we find the StringUtils.

As the name indicates, it’s a utility class that provides a convenient way to operate on strings in a null-safe manner.

 String str = "bar"; int strLength = StringUtils.length(str); for (int i = 0; i for (int j = i + 1; j .out.println(StringUtils.substring(str, i, j)); > > 

As shown above, we used StringUtils.length() to get the length of the original string.

Then, we used the StringUtils.substring() method to find all the possible substrings.

Find all Unique Substrings

Now, let’s see how to print all the possible unique subregions of a particular string.

Typically, we can use a HashSet implementation to achieve this. By design, HashSet does not accept duplicate values:

 import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class UniqSubstrings < public UniqSubstrings() < >public static void main(String[] args) < String str = "deev"; SetString> uniqSubs = new HashSetString>(); for (int i = 0; i for (int j = i + 1; j > IteratorString> siterator = uniqSubs.iterator(); while(siterator.hasNext())< System.out.println(siterator.next()); > > > 

The above program will output:

 ee de ev d dee e v eev deev 

Conclusion:

To sum it up, we have showcased how to find all substrings of a string in Java.

Along the way, we illustrated how to do this using built-in Java methods and classes.

Then, we explained how to accomplish the same objective using Apache Commons library.

Liked the Article? Share it on Social media!

If you enjoy reading my articles, buy me a coffee ☕. I would be very grateful if you could consider my request ✌️

Источник

all substrings of a string in java

How to find all possible substrings of a string in Java?.For example I have a string say «abc» then i want to generate all possible substrings of abc that is:-«a»,»b»,»c»,»ab»,»bc»,»ac»,»abc».The above code generates all possible substrings except «ac».Can anyone give the code?

Well what call to «abc».substring(. ) would create it? What do you think «abc».indexOf(«ac») would return?

unless what you want is all combinations of the characters in the string in the forward order. Is that what you’re looking for?

3 Answers 3

public class FindAllSubStrings < public static ArrayListlist = new ArrayList(); public static void main(String args[]) < String str = "abcdef"; findSubString(str); >public static void findSubString(String str) < for(int i=0;iString temp =""; for(int i=0;i > printAll(); > public static void printAll() < for(int i =0;i> 

This will help, iterate i as begin of the substring, and j as the end of the substring.

String s1 = "abcdefg"; for (int i = 0;i < s1.length - 1;i ++) < for (int j = i + 1;j < s1.length;j ++) < arr[k ++] = s1.substring(i, j); >> 

Answer As per the question The above code generates all possible substrings except «ac».Can anyone give the code?

Instead of word substring it should be all combinations

Following example will give all possible combinations of string:

 public class StringCombinations < private StringBuilder output = new StringBuilder(); private final String inputstring; public StringCombinations(final String str) < inputstring = str; System.out.println("The input string is : " + inputstring); >public static void main(String args[]) < StringCombinations combobj = new StringCombinations("abc"); System.out.println("All possible combinations are : "); combobj.combine(); >public void combine() < combine(0); >private void combine(int start) < for (int i = start; i < inputstring.length(); ++i) < output.append(inputstring.charAt(i)); System.out.println(output); if (i < inputstring.length()) combine(i + 1); output.setLength(output.length() - 1); >> > 

Источник

how to get all substring for a given regex?

I need to get all substrings matching a regex, I know I can probably build an automaton for it, but I am looking for a simpler solution.
the problem is, Matcher.find() doesn’t return all results.

String str = "abaca"; Matcher matcher = Pattern.compile("a.a").matcher(str); while (matcher.find())

The result is aba and not aba,aca as I want.
any ideas?
EDIT: another example: for string=abaa, regex=a.*a I am expecting to get aba,abaa,aa
p.s. if it cannot be achieved using regular expressions, it’s also an answer, I just want to know I’m not re-inventing the wheel for something the language already provides me with.

The problem is that the matcher only considers non-overlapping matches. Still, this is an interesting problem. +1

4 Answers 4

You could do something like this:

import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main < public static ListgetAllMatches(String text, String regex) < Listmatches = new ArrayList(); Matcher m = Pattern.compile("(?=(" + regex + "))").matcher(text); while(m.find()) < matches.add(m.group(1)); >return matches; > public static void main(String[] args) < System.out.println(getAllMatches("abaca", "a.a")); System.out.println(getAllMatches("abaa", "a.*a")); >> 

The only thing is that you’re missing aba from the last matches-list. This is because of the greedy .* in a.*a . You can’t fix this with regex. You could do this by iterating over all possible substrings and call .matches(regex) on each substring:

public static List getAllMatches(String text, String regex) < Listmatches = new ArrayList(); for(int length = 1; length > > return matches; > 

If your text will stay relatively small, this will work, but for larger strings, this may become too computationally intense.

Источник

Оцените статью