How to merge a list of string word by word? [duplicate]










-3
















This question already has an answer here:



  • Best way to convert an ArrayList to a string

    28 answers



It is an interview question. The input is an ArrayList. My first idea is to convert it into a 2D matrix and then combine each column, but it seem like not the right answer. Is there any other way to solve this? Thanks.



Input



"abc", 
"bef",
"g"


Expected Output (first column, abg, then the second column, be and finally the third column, cf):



"abgbecf"









share|improve this question















marked as duplicate by GBlodgett, ggorlen, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 17:00


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Specifically looking at the second answer: String listString = String.join("", list); stackoverflow.com/a/23183963/8534008

    – GBlodgett
    Nov 13 '18 at 19:30












  • input order and output order are different, or order is not mandatory ?

    – Deadpool
    Nov 13 '18 at 19:35






  • 1





    loop from 0 to max(lengths of the words). At each iteration, loop through each word and append the nth character (if it exists) of the current word to a StringBuilder. Done.

    – JB Nizet
    Nov 13 '18 at 19:35
















-3
















This question already has an answer here:



  • Best way to convert an ArrayList to a string

    28 answers



It is an interview question. The input is an ArrayList. My first idea is to convert it into a 2D matrix and then combine each column, but it seem like not the right answer. Is there any other way to solve this? Thanks.



Input



"abc", 
"bef",
"g"


Expected Output (first column, abg, then the second column, be and finally the third column, cf):



"abgbecf"









share|improve this question















marked as duplicate by GBlodgett, ggorlen, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 17:00


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Specifically looking at the second answer: String listString = String.join("", list); stackoverflow.com/a/23183963/8534008

    – GBlodgett
    Nov 13 '18 at 19:30












  • input order and output order are different, or order is not mandatory ?

    – Deadpool
    Nov 13 '18 at 19:35






  • 1





    loop from 0 to max(lengths of the words). At each iteration, loop through each word and append the nth character (if it exists) of the current word to a StringBuilder. Done.

    – JB Nizet
    Nov 13 '18 at 19:35














-3












-3








-3









This question already has an answer here:



  • Best way to convert an ArrayList to a string

    28 answers



It is an interview question. The input is an ArrayList. My first idea is to convert it into a 2D matrix and then combine each column, but it seem like not the right answer. Is there any other way to solve this? Thanks.



Input



"abc", 
"bef",
"g"


Expected Output (first column, abg, then the second column, be and finally the third column, cf):



"abgbecf"









share|improve this question

















This question already has an answer here:



  • Best way to convert an ArrayList to a string

    28 answers



It is an interview question. The input is an ArrayList. My first idea is to convert it into a 2D matrix and then combine each column, but it seem like not the right answer. Is there any other way to solve this? Thanks.



Input



"abc", 
"bef",
"g"


Expected Output (first column, abg, then the second column, be and finally the third column, cf):



"abgbecf"




This question already has an answer here:



  • Best way to convert an ArrayList to a string

    28 answers







java algorithm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 20:20









Bart Kiers

131k28243248




131k28243248










asked Nov 13 '18 at 19:29









RubyARubyA

1




1




marked as duplicate by GBlodgett, ggorlen, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 17:00


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by GBlodgett, ggorlen, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 17:00


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Specifically looking at the second answer: String listString = String.join("", list); stackoverflow.com/a/23183963/8534008

    – GBlodgett
    Nov 13 '18 at 19:30












  • input order and output order are different, or order is not mandatory ?

    – Deadpool
    Nov 13 '18 at 19:35






  • 1





    loop from 0 to max(lengths of the words). At each iteration, loop through each word and append the nth character (if it exists) of the current word to a StringBuilder. Done.

    – JB Nizet
    Nov 13 '18 at 19:35


















  • Specifically looking at the second answer: String listString = String.join("", list); stackoverflow.com/a/23183963/8534008

    – GBlodgett
    Nov 13 '18 at 19:30












  • input order and output order are different, or order is not mandatory ?

    – Deadpool
    Nov 13 '18 at 19:35






  • 1





    loop from 0 to max(lengths of the words). At each iteration, loop through each word and append the nth character (if it exists) of the current word to a StringBuilder. Done.

    – JB Nizet
    Nov 13 '18 at 19:35

















Specifically looking at the second answer: String listString = String.join("", list); stackoverflow.com/a/23183963/8534008

– GBlodgett
Nov 13 '18 at 19:30






Specifically looking at the second answer: String listString = String.join("", list); stackoverflow.com/a/23183963/8534008

– GBlodgett
Nov 13 '18 at 19:30














input order and output order are different, or order is not mandatory ?

– Deadpool
Nov 13 '18 at 19:35





input order and output order are different, or order is not mandatory ?

– Deadpool
Nov 13 '18 at 19:35




1




1





loop from 0 to max(lengths of the words). At each iteration, loop through each word and append the nth character (if it exists) of the current word to a StringBuilder. Done.

– JB Nizet
Nov 13 '18 at 19:35






loop from 0 to max(lengths of the words). At each iteration, loop through each word and append the nth character (if it exists) of the current word to a StringBuilder. Done.

– JB Nizet
Nov 13 '18 at 19:35













3 Answers
3






active

oldest

votes


















0














Something like this would work:



StringBuilder sb = new StringBuilder()
int max = 0;
for(String str : arrList)
if(str.length > max)
max = str.length;


for(int i = 0; i < max; i++)
for(String str : arrList)
if(str.length > i)
sb.append(str.charAt(i));



return sb.toString();





share|improve this answer






























    0














    I think I would simply loop through the list pulling the next character out of the original strings and adding it to the new combined string, until the last letter of the longest string was added.



    boolean keepGoing = true;
    int index = 0;
    StringBuilder result = new StringBuilder();
    while(keepGoing)
    keepGoing = false;
    for(int i=0; i < stringList.size(); i++)
    if(stringList.get(i).length() > index)
    result.append(stringList.get(i).charAt(index));
    keepGoing = true;


    index++;

    System.out.println("result: " + result);


    There are probably more elegant solutions, but this is what I would start with and then refine as needed.






    share|improve this answer






























      -1














      An easy way would be to use a StringBuilder:



      StringBuilder sb = new StringBuilder()
      for(String str : arrList)
      sb.append(str)

      return sb.toString()





      share|improve this answer





























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        Something like this would work:



        StringBuilder sb = new StringBuilder()
        int max = 0;
        for(String str : arrList)
        if(str.length > max)
        max = str.length;


        for(int i = 0; i < max; i++)
        for(String str : arrList)
        if(str.length > i)
        sb.append(str.charAt(i));



        return sb.toString();





        share|improve this answer



























          0














          Something like this would work:



          StringBuilder sb = new StringBuilder()
          int max = 0;
          for(String str : arrList)
          if(str.length > max)
          max = str.length;


          for(int i = 0; i < max; i++)
          for(String str : arrList)
          if(str.length > i)
          sb.append(str.charAt(i));



          return sb.toString();





          share|improve this answer

























            0












            0








            0







            Something like this would work:



            StringBuilder sb = new StringBuilder()
            int max = 0;
            for(String str : arrList)
            if(str.length > max)
            max = str.length;


            for(int i = 0; i < max; i++)
            for(String str : arrList)
            if(str.length > i)
            sb.append(str.charAt(i));



            return sb.toString();





            share|improve this answer













            Something like this would work:



            StringBuilder sb = new StringBuilder()
            int max = 0;
            for(String str : arrList)
            if(str.length > max)
            max = str.length;


            for(int i = 0; i < max; i++)
            for(String str : arrList)
            if(str.length > i)
            sb.append(str.charAt(i));



            return sb.toString();






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 19:53









            The Zach ManThe Zach Man

            1176




            1176























                0














                I think I would simply loop through the list pulling the next character out of the original strings and adding it to the new combined string, until the last letter of the longest string was added.



                boolean keepGoing = true;
                int index = 0;
                StringBuilder result = new StringBuilder();
                while(keepGoing)
                keepGoing = false;
                for(int i=0; i < stringList.size(); i++)
                if(stringList.get(i).length() > index)
                result.append(stringList.get(i).charAt(index));
                keepGoing = true;


                index++;

                System.out.println("result: " + result);


                There are probably more elegant solutions, but this is what I would start with and then refine as needed.






                share|improve this answer



























                  0














                  I think I would simply loop through the list pulling the next character out of the original strings and adding it to the new combined string, until the last letter of the longest string was added.



                  boolean keepGoing = true;
                  int index = 0;
                  StringBuilder result = new StringBuilder();
                  while(keepGoing)
                  keepGoing = false;
                  for(int i=0; i < stringList.size(); i++)
                  if(stringList.get(i).length() > index)
                  result.append(stringList.get(i).charAt(index));
                  keepGoing = true;


                  index++;

                  System.out.println("result: " + result);


                  There are probably more elegant solutions, but this is what I would start with and then refine as needed.






                  share|improve this answer

























                    0












                    0








                    0







                    I think I would simply loop through the list pulling the next character out of the original strings and adding it to the new combined string, until the last letter of the longest string was added.



                    boolean keepGoing = true;
                    int index = 0;
                    StringBuilder result = new StringBuilder();
                    while(keepGoing)
                    keepGoing = false;
                    for(int i=0; i < stringList.size(); i++)
                    if(stringList.get(i).length() > index)
                    result.append(stringList.get(i).charAt(index));
                    keepGoing = true;


                    index++;

                    System.out.println("result: " + result);


                    There are probably more elegant solutions, but this is what I would start with and then refine as needed.






                    share|improve this answer













                    I think I would simply loop through the list pulling the next character out of the original strings and adding it to the new combined string, until the last letter of the longest string was added.



                    boolean keepGoing = true;
                    int index = 0;
                    StringBuilder result = new StringBuilder();
                    while(keepGoing)
                    keepGoing = false;
                    for(int i=0; i < stringList.size(); i++)
                    if(stringList.get(i).length() > index)
                    result.append(stringList.get(i).charAt(index));
                    keepGoing = true;


                    index++;

                    System.out.println("result: " + result);


                    There are probably more elegant solutions, but this is what I would start with and then refine as needed.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 '18 at 20:05









                    BlairBlair

                    665




                    665





















                        -1














                        An easy way would be to use a StringBuilder:



                        StringBuilder sb = new StringBuilder()
                        for(String str : arrList)
                        sb.append(str)

                        return sb.toString()





                        share|improve this answer



























                          -1














                          An easy way would be to use a StringBuilder:



                          StringBuilder sb = new StringBuilder()
                          for(String str : arrList)
                          sb.append(str)

                          return sb.toString()





                          share|improve this answer

























                            -1












                            -1








                            -1







                            An easy way would be to use a StringBuilder:



                            StringBuilder sb = new StringBuilder()
                            for(String str : arrList)
                            sb.append(str)

                            return sb.toString()





                            share|improve this answer













                            An easy way would be to use a StringBuilder:



                            StringBuilder sb = new StringBuilder()
                            for(String str : arrList)
                            sb.append(str)

                            return sb.toString()






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 13 '18 at 19:47









                            Turtalicious Turtalicious

                            524




                            524













                                這個網誌中的熱門文章

                                How to read a connectionString WITH PROVIDER in .NET Core?

                                In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                                Museum of Modern and Contemporary Art of Trento and Rovereto