How to merge a list of string word by word? [duplicate]
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"
java algorithm
marked as duplicate by GBlodgett, ggorlen, Mark Rotteveel
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.
add a comment |
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"
java algorithm
marked as duplicate by GBlodgett, ggorlen, Mark Rotteveel
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
add a comment |
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"
java algorithm
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
java algorithm
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
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
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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();
add a comment |
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.
add a comment |
An easy way would be to use a StringBuilder:
StringBuilder sb = new StringBuilder()
for(String str : arrList)
sb.append(str)
return sb.toString()
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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();
add a comment |
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();
add a comment |
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();
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();
answered Nov 13 '18 at 19:53
The Zach ManThe Zach Man
1176
1176
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 13 '18 at 20:05
BlairBlair
665
665
add a comment |
add a comment |
An easy way would be to use a StringBuilder:
StringBuilder sb = new StringBuilder()
for(String str : arrList)
sb.append(str)
return sb.toString()
add a comment |
An easy way would be to use a StringBuilder:
StringBuilder sb = new StringBuilder()
for(String str : arrList)
sb.append(str)
return sb.toString()
add a comment |
An easy way would be to use a StringBuilder:
StringBuilder sb = new StringBuilder()
for(String str : arrList)
sb.append(str)
return sb.toString()
An easy way would be to use a StringBuilder:
StringBuilder sb = new StringBuilder()
for(String str : arrList)
sb.append(str)
return sb.toString()
answered Nov 13 '18 at 19:47
Turtalicious Turtalicious
524
524
add a comment |
add a comment |
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