Print all the substrings of a given string in order by size

不羁的心 提交于 2021-02-08 11:11:26

问题


This code I'm using is from this previously asked question. This question has been asked and answered plenty of times but I'm specifically asking for the order to be listed by size from largest to smallest.

public static void main(String[] args)
{
    String inputedWord = "ABIGWORD";

    for (String str : breakStringIntoPieces(inputedWord, 2))
    {
        System.out.print("\n") + str;
    }
}

                                                            //Pass in word and minimum
                                                            //substring length to print
public static List<String> breakStringIntoAllPossibleSubstrings(String str, int num)
{                                                               
    List<String> listOfSubstrings = new ArrayList<>();
    Boolean insideLoop = false;

    for(int i=0; i<=str.length()-num; i++)
    {
        for(int j=str.length(); j>=i+num; j--)
        {
            //System.out.println(str.substring(i, j));
            if (insideLoop) //This is simply to not add the complete string to the 
            {               //list. Only substrings

                listOfSubstrings.add(str.substring(i, j));
            }
            insideLoop = true;
        }
    }
    return listOfSubstrings;
}

OUTPUT:

ABIGWOR
ABIGWO
ABIGW
ABIG
ABI
AB

BIGWORD
BIGWOR
BIGWO
BIGW
BIG
BI

IGWORD
IGWOR
IGWO
IGW
IG

GWORD
GWOR
GWO
GW

WORD
WOR
WO

ORD
OR

RD

DESIRED OUTPUT: (In no special order other than size. This is just a typed example.

ABIGWOR
BIGWORD
ABIGWO
BIGWOR
IGWORD
GWORD
ABIGW
IGWOR
BIGWO
IGWO
ABIG
BIGW
WORD
GWOR
GWO
ORD
ABI
BIG
IGW
WOR
AB
BI
IG
GW
WO
OR
RD

I could technically just loop through the returned list and find all the biggest substrings but that would add too many steps. I'm wondering if there's away to do it within the given method. I think the process involves manipulated the i and j iterators after each loop?


回答1:


One simple way to achieve this with minimal changes would be to sort the listOfSubstrings ArrayList by length and then return the result. This will just be a one line change using Collections.sort.

public static List<String> breakStringIntoAllPossibleSubstrings(String str, int num) {                                                               
    List<String> listOfSubstrings = new ArrayList<>();

    /* Your code added here... */

    // This will sort in descending order of length
    Collections.sort(listOfSubstrings, (item1, item2) -> item2.length() - item1.length());

    return listOfSubstrings;
}

In terms of time complexity, for a String of size N, generating all substrings is of the order O(N^2).

An extra sorting operation will introduce O(N^2 x log(N^2)) = O(N^2 x log(N)).

Therefore, overall complexity will be O(N^2 x log(N))



来源:https://stackoverflow.com/questions/61357341/print-all-the-substrings-of-a-given-string-in-order-by-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!