You are not logged in. Please login at www.codechef.com to post your questions!

×

Create a list of String[], in each String[] the last char of an element must be the first char of the next element

I have a String[] { "honorifi", "cabilitudi", "nitatibus", "antidisestab", "lishmenta", "rianism", "floccinau", "cihihilipil", "ification", "supercalifrag", "ilisticexpia" }

I expect an output is array of string[]. In each array, the last char of this element will equal to the first char of the next element. The output must be like this:

["honorifi", "ification", "nitatibus", "supercalifrag"]
["honorifi", "ilisticexpia", "antidisestab"]
["cabilitudi", "ification", "nitatibus", "supercalifrag"]
["cabilitudi", "ilisticexpia", "antidisestab"]
["nitatibus", "supercalifrag"]
["antidisestab"]
["lishmenta", "antidisestab"]
["rianism"]
["floccinau"]
["cihihilipil", "lishmenta", "antidisestab"]
["ification", "nitatibus", "supercalifrag"]
["supercalifrag"]
["ilisticexpia", "antidisestab"]

However, my code misses some cases of result.

[honorifi, ification, nitatibus, supercalifrag] [cabilitudi, ification, nitatibus, supercalifrag] [nitatibus, supercalifrag] [antidisestab] [lishmenta, antidisestab] [rianism] [floccinau] [cihihilipil, lishmenta, antidisestab] [ification, nitatibus, supercalifrag] [supercalifrag] [ilisticexpia, antidisestab]

Please review my code and help me to correct it. Thank you so much

static String[] test;

public static void main(String[] args) throws java.lang.Exception {
    // your code goes here
    test = new String[] { "honorifi", "cabilitudi", "nitatibus", "antidisestab", "lishmenta", "rianism", "floccinau", "cihihilipil", "ification", "supercalifrag", "ilisticexpia" };

    ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();

    for (String string : test) {
        ArrayList<String> suite = sortSmartly(string);
        data.add(suite);
    }

    for (ArrayList<String> arrayList : data) {
        System.out.println(Arrays.toString(arrayList.toArray()));
    }
}

public static ArrayList<String> sortSmartly(String text) {
    ArrayList<String> result = new ArrayList<String>();
    result.add(text);
    String lastChar = text.substring(text.length() - 1);
    for (String object : test) {
        String firstChar = object.substring(0, 1);
        if (lastChar.equals(firstChar)) {
            result.addAll(sortSmartly(object));
            break;
        }
    }
    return result;
}

asked 11 Jul, 22:38

phamhiepst's gravatar image

0★phamhiepst
11
accept rate: 0%

edited 11 Jul, 22:39

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×760
×548

question asked: 11 Jul, 22:38

question was seen: 27 times

last updated: 11 Jul, 22:39