-3

I want to dynamically code for any no of strings.

For example if I am taking three strings "a" "b" "c"

I would like to get as result:

String []array= {"a", "b", "c", "a b", "b c", "a b c"};

(lack of "a c" is intended)

For four strings "a" "b" "c" "d" I would like to get as result

"a" "b" "c" "d" "a b" "b c" "c d" "a b c" "b c d" "a b c d"

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Maqsood Hakro
  • 27
  • 1
  • 8
  • What happened with `"a c"`? – Pshemo Jun 12 '16 at 14:47
  • no sir i don't need "a c". Just i want output like "a" "b" "c" "a b" "b c" "abc". – Maqsood Hakro Jun 12 '16 at 20:12
  • Sir if i take four strings then output should be ... abcd= "a" "b" "c" "d" "a b" "b c" "c d" "a b c" "b c d" "a b c d" – Maqsood Hakro Jun 12 '16 at 20:13
  • Oh, so it wasn't simple mistake in question. In that case I will reopen it as pointed duplicate doesn't fulfil your requirements. – Pshemo Jun 12 '16 at 20:17
  • Can you explain "dynamically code"? Do you want your IDE to automatically generate code for such array based on some input? Or do you mean that you would like to generate such results in Java with simple methods and input like array `{"a","b","c"}`? – Pshemo Jun 12 '16 at 20:24
  • dynamically means sir we can give any no of strings and generate result. yes sir , i only want to generate such result in java. – Maqsood Hakro Jun 12 '16 at 20:37
  • For Example: a= "a" ab= "a" "b" "ab" abc= "a" "b" "c" "a b" "b c" "a b c" abcd= "a" "b" "c" "a b" "b c" "c d" "a b c" "b c d" "a b c d" – Maqsood Hakro Jun 12 '16 at 20:39

3 Answers3

0

So you want a set of all consecutive concatenations strings.

Say your input is an array of strings s = {s0, s1, s2, ..., sn} Now your result array of strings r will contain all elements of s.

r = s.copy()

now you add all consecutive concatenations with 2 strings

  for i = 0; i < n-1; i++;
    r.add(s[i] + s[i+1])

continue with all consecutive concatenations of 3 strings up to n strings

xuma202
  • 1,074
  • 1
  • 10
  • 22
  • does this generate if we give abcd = "a" "b" "c" "a b" "b c" "c d" "a b c" "b c d" "a b c d" – Maqsood Hakro Jun 12 '16 at 20:54
  • Welk this is a pseudo description of an implementation that would generate that output. You'll bee a for loop from 1-n (length of generated sequence) and an inner loop fork 0-(n-i) somewhat that shifts trough your values – xuma202 Jun 13 '16 at 22:10
0

Since this looks like homework I will try to explain solution but you are the one who needs to create code.


If we have elements like

a, b, c, d

tokens using 1 elements will be

a b c d 
^ ^ ^ ^

tokens using 2 elements will be

a b c d
^^^
  ^^^
    ^^^

tokens using 3 elements will be

a b c d
^^^^^
  ^^^^^

and token using all 4 element will be

a b c d
^^^^^^^

So in case of each token of size n we are iterating from a, b, and adding to it its next n-1 elements.

So method which will generate all tokens of length n should work like this:

  • for all elements from which we can create full tokens (if token size is 2 we can't use last element, if its size is 3 we can't use last 2 elements, ...)
    • get next n-1 elements and combine them in single token (collect that token in some collection so you can return it later)

Assuming that such method will be declared like List<String> generateTokens(String[] elements, int tokenSize)

for case

String[] elements = { "a", "b", "c", "d" };

we would need to invoke it 4 times to get

generateTokens(elements, 1) // ["a", "b", "c", "d"]
generateTokens(elements, 2) // ["a b", "b c", "c d"]
generateTokens(elements, 3) // ["a b c", "b c d"]
generateTokens(elements, 4) // ["a b c d"]

So as you see you need to invoke that method x times (where x is amount of elements) and gather in one place all results. You can easily achieve that with simple loop.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • yes sir this is perfect for my solution please make method generateTokens() – Maqsood Hakro Jun 12 '16 at 21:06
  • @MaqsoodHakro Sorry but please read first sentence of my answer. Try to implement it first and when you will have some specific problem feel free to ask about it (preferably after searching first for more than at least 15 minutes). – Pshemo Jun 12 '16 at 21:09
  • @MaqsoodHakro Please read also this [Open letter to students with homework problems](http://meta.programmers.stackexchange.com/q/6166) – Pshemo Jun 12 '16 at 21:09
  • I can't make please make it . It is so important for me please help me. – Maqsood Hakro Jun 13 '16 at 14:10
  • String st = "a b c"; genTokens(st){ return array[]={"a","b","a b","b c", "a b c"}; } – Maqsood Hakro Jun 13 '16 at 14:11
  • @MaqsoodHakro You need to be more specific. I gave you recipe and your task is to make cake. If some part of that recipe is not clear then ask about it. You will not learn much if others will simply give you solutions to all problems. You need to learn how they are able to create such solutions so you can create them on your own and to do so you need to go as far as you can first and then describe specific problem which is stopping you from moving farther. – Pshemo Jun 13 '16 at 15:09
  • 1
    Sir I have made it as i wanted. and i learned lot of things and i am happy now thankyou for motivation Sir again thankyou. – Maqsood Hakro Jun 14 '16 at 09:42
  • @MaqsoodHakro You are very welcome! Good luck with your future projects. – Pshemo Jun 14 '16 at 10:09
0

//Just you have to call possibleSetsOfString() to make possible sets.. and it will give u the possible sets in a one list...

public String concitinate(String st, int start, int end) {

    int i = start;
    int j = end + 1;
    entities = new ArrayList<String>(Arrays.asList(st.trim().split("\\s+")));

    String p = "";
    String num = "";
    for (int k = i; k < j; k++) {

        p += entities.get(k) + " ";

        num += +k + ",";


    }
    numbers.add(num);




    p = p.replaceAll("( )+", " ");
    return p.trim();
}

public ArrayList<String> possibleSetsOfString(String txt) {
    ArrayList<String> mylist = new ArrayList<String>();
    lengthOfString = countWords(txt);

    int k = 0;
    // int j=0;
    for (int i = 0; i < lengthOfString; i++) {
        for (int j = 0; j < lengthOfString - k; j++) {
            String s = concitinate(txt, j, j + k);
            mylist.add(s);


        }
        k++;
    }




    // Collections.sort(mylist, Collections.reverseOrder());

    Collections.reverse(mylist);
    Collections.reverse(numbers);

    return mylist;

}
Maqsood Hakro
  • 27
  • 1
  • 8