Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A more succinct answer #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions java/string-2/mixString.java
Original file line number Diff line number Diff line change
@@ -3,24 +3,11 @@
* and so on. Any leftover chars go at the end of the result.
*/
public String mixString(String a, String b) {
char[] arr;
String end;
int count = 0;

if(a.length() < b.length()) {
arr = new char[2 * a.length()];
end = b.substring(a.length());
} else {
arr = new char[2 * b.length()];
end = a.substring(b.length());
}

for(int i = 0; i < arr.length / 2; i++) {
arr[count] = a.charAt(i);
count++;
arr[count] = b.charAt(i);
count++;
}

return new String(arr) + end;
StringBuffer mix = new StringBuffer();
int len = (a.length() > b.length())? a.length(): b.length();
for(int i = 0; i < len; i++){
if(i < a.length()) mix.append(a.charAt(i));
if(i < b.length()) mix.append(b.charAt(i));
}
return mix.toString();
}