Problem:
Given two strings, A and B, create a bigger string made of the first char of A, the first char of B, the second char of A, the second char of B, and so on. Any leftover chars go at the end of the result.
mixString("abc", "xyz") → "axbycz"
mixString("Hi", "There") → "HTihere"
mixString("xxxx", "There") → "xTxhxexre"
Solution:
public String mixString(String a, String b) { int aLen = a.length(); int bLen = b.length(); int max = Math.max(aLen, bLen); String word = ""; for (int i = 0; i < max; i++) { if (i <= aLen-1) word += a.substring(i,i+1); if (i <= bLen-1) word += b.substring(i,i+1); } return word; }
I invented the wheel...
ReplyDeletepublic String mixString(String a, String b) {
char[] result = new char[a.length() + b.length()];
int i = 0;
int aK = 0;
int bK = 0;
while (i < result.length){
if (aK < a.length()) {
result[i] = a.charAt(aK);
aK++;
i++;
}
if (bK < b.length()) {
result[i] = b.charAt(bK);
bK++;
i++;
}
}
String res = new String(result);
return res;
}
Kon'nichiwa sensei!
Deletepublic String mixString(String a, String b) {
ReplyDeleteint stringALen = a.length();
int stringBLen = b.length();
int shortStr = 0;
String newStr = "";
if(stringALen >= stringBLen) shortStr = stringBLen;
if(stringBLen >= stringALen) shortStr = stringALen;
if(stringALen >= stringBLen){
for(int i = 0; i < shortStr; i++ ){
newStr += a.charAt(i);
newStr += b.charAt(i);
}
newStr = newStr + a.substring(shortStr);
}
if(stringBLen > stringALen){
for(int i = 0; i < shortStr; i++ ){
newStr += a.charAt(i);
newStr += b.charAt(i);
}
newStr = newStr + b.substring(shortStr);
}
return newStr;
}
public String mixString(String a, String b) {
ReplyDeleteString result = "";
int i = 0;
while (i<a.length()&&i<b.length()){
result = result + a.charAt(i) + b.charAt(i);
i++;
}
return result+a.substring(i)+b.substring(i);
}
not working
Deleteit works perfectly.
Deleteto use this code in other editor
class mix
{
public String mixString(String a, String b) {
String result = "";
int i = 0;
while (i<a.length()&&i<b.length()){
result = result + a.charAt(i) + b.charAt(i);
i++;
}
return result+a.substring(i)+b.substring(i);
}
public static void main(String[] args)
{
String a1;
mix b1= new mix();
a1=b1.mixString("he","lo");
System.out.println(a1);
}
}
public String mixString(String a, String b) {
ReplyDeleteint min = Math.min(a.length(), b.length());
String result = "";
String tail = "";
for(int i = 0; i < min; i++){
result = result + a.charAt(i) + b.charAt(i);
}
if(a.length() < b.length()){
tail = b.substring(min, b.length());
result = result + tail;
}
if(a.length() > b.length()){
tail = a.substring(min, a.length());
result = result + tail;
}
return result;
}
Wow. We used almost exact same logic
DeleteString blank = "";
int lenA = a.length();
int lenB = b.length();
int minLen = Math.min(lenA,lenB);
for(int i=0; i=lenB){
blank = blank + a.substring(lenB);
}
return blank;
Wrong paste
Deletepublic String mixString(String a, String b) {
String blank = "";
int lenA = a.length();
int lenB = b.length();
int minLen = Math.min(lenA,lenB);
for(int i=0; i=lenB){
blank = blank + a.substring(lenB);
}
return blank;
}
String str=""; int i=0;
ReplyDeleteif(a.length()<=0)
{
return b;
}
else if(b.length()<=0)
{
return a;
}
else if(a.length()<b.length())
{
for(i=0;i<a.length();i++)
{
str = str+ a.substring(i,i+1)+b.substring(i,i+1);
}
return str+b.substring(i);
}
else
{
for(i=0;i<b.length();i++)
{
str = str+ a.substring(i,i+1)+b.substring(i,i+1);
}
return str+a.substring(i);
}
public String mixString(String a, String b) {
ReplyDeleteString result = "";
if (a.length()<=b.length())
{
for (int i=0; i<a.length(); i++)
{
result = result + a.charAt(i) + b.charAt(i);
}
result = result + b.substring(a.length(),b.length());
}
if (b.length()<a.length())
{
for (int i=0; i<b.length(); i++)
{
result = result + a.charAt(i) + b.charAt(i);
}
result = result + a.substring(b.length(),a.length());
}
return result;
}
public String mixString(String a, String b) {
ReplyDeleteString newString = "";
int minLen = Math.min(a.length(),b.length());
for(int i=0; i<minLen; i++){
newString = newString + a.charAt(i) + b.charAt(i);
}
if(minLen == a.length())
newString = newString + b.substring(minLen);
if(minLen == b.length())
newString = newString + a.substring(minLen);
return newString;
}
I really need to remember that the Math class exists ._.
ReplyDeletepublic String mixString(String a, String b) {
String result = "";
int lengthA = a.length();
int lengthB = b.length();
if(lengthA > lengthB){
for(int x = 0 ; x < lengthB ; x++){
result = result + a.substring(x,x+1);
result = result + b.substring(x,x+1);
}
result = result + a.substring(lengthB);
}
else if(lengthA < lengthB){
for(int x = 0 ; x < lengthA ; x++){
result = result + a.substring(x,x+1);
result = result + b.substring(x,x+1);
}
result = result + b.substring(lengthA);
}
else{
for(int x = 0 ; x < lengthA ; x++){
result = result + a.substring(x,x+1);
result = result + b.substring(x,x+1);
}
}
return result;
}
public String mixString(String a, String b) {
ReplyDeleteint alen = a.length();
int blen = b.length();
String newStr = "";
if (alen == blen)
{
for (int i = 0 ; i < alen ; i++)
{
newStr = newStr + a.charAt(i) + b.charAt(i);
}
}
if (alen < blen)
{
for (int i = 0; i < alen; i++)
{
newStr = newStr+ a.charAt(i) + b.charAt(i) ;
}
newStr= newStr + b.substring(alen,blen);
}
if (alen > blen)
{
for (int i = 0; i < blen; i++)
{
newStr = newStr + a.charAt(i) + b.charAt(i) ;
}
newStr= newStr + a.substring(blen,alen);
}
return newStr;
}
public String mixString(String a, String b) {
ReplyDeleteString s="";
if(a.length()<=b.length())
{
for(int i=0;ib.length())
{
for(int i=0;i<b.length();i++)
{
s=s+a.charAt(i)+b.charAt(i);
}
s=s+a.substring(b.length(),a.length());
}
return s;
}
public String mixString(String a, String b) {
ReplyDeleteint l = 0;
String res = "";
String h = "";
if (a.length() <= b.length()) {
l = a.length();
h = b.substring(l);
} else {
l = b.length();
h = a.substring(l);
}
for (int i = 0; i < l; i++) {
res += a.substring(i, i+1) + b.substring(i, i+1);
}
return res + h;
}
get a star from coding bat:
ReplyDeletepublic String mixString(String a, String b) {
int max, min;
if(a.length() > b.length())
{
max = a.length();
min = b.length();
}
else
{
max = b.length();
min = a.length();
}
String ab = "";
for(int c = 0; c < min; c++)
{
ab = ab + a.charAt(c) + b.charAt(c);
}
if(max == a.length())
{
for(int d = min; d < max; d++)
{
ab = ab + a.charAt(d);
}
}
else
{
for(int d = min; d < max; d++)
{
ab = ab + b.charAt(d);
}
}
return ab;
}
public String mixString(String a, String b) {
ReplyDeleteif(a.length()== 0)
return b;
else if(b.length() == 0)
return a;
else
return "" + a.charAt(0) + b.charAt(0) + mixString(a.substring(1), b.substring(1));
}
Nice!
Deletepublic String mixString(String a, String b) {
ReplyDeleteString temp="";
int minimum =Math.min(a.length(),b.length());
for (int i=0;ib.length())
temp+=a.substring(minimum);
return temp;
}
public String mixString(String a, String b) {
ReplyDeleteString temp="";
int minimum =Math.min(a.length(),b.length());
for (int i=0;ib.length())
temp+=a.substring(minimum);
return temp;
}
public String mixString(String a, String b) {
ReplyDeleteint small = (a.length() > b.length() ? b.length() : a.length());
String big = (a.length() > b.length() ? a : b);
String result = "";
for (int i = 0; i < small; i++) {
result = result + a.charAt(i) + b.charAt(i);
}
return result + big.substring(small);
}
public String mixString(String a, String b) {
ReplyDeleteString result = "";
int len = 0;
if ( a.length() <= b.length()) {
len = a.length();
} else if ( a.length() > b.length()){
len = b.length();
}
for ( int i = 0; i < len ; i++ ){
result = result + a.charAt(i) + b.charAt(i);
}
if ( a.length() < b.length()) {
result += b.substring(a.length());
} else {
result += a.substring(b.length());
}
return result;
}
public String mixString(String a, String b) {
ReplyDeleteint len = Math.min(a.length(), b.length());
String word = "";
for(int i=0; ib.length())
{
word = word + a.substring(len, a.length());
}
if(b.length()>a.length())
{
word = word + b.substring(len, b.length());
}
return word;
}
public String mixString(String a, String b) {
ReplyDeleteString word = "";
int max = Math.max(a.length(),b.length());
for(int i=0; i<max; i++){
if(i<a.length())
word+=a.charAt(i);
if(i<b.length())
word+=b.charAt(i);
}
return word;
}
public String mixString(String a, String b) {
ReplyDeleteString rep = "";
for (int i = 0; i < Math.max(a.length(), b.length()); i++)
{
if (i < a.length())
{
rep += a.substring(i, i + 1);
}
if (i < b.length())
{
rep += b.substring(i, i + 1);
}
}
return rep;
}
public String mixString(String a, String b) {
ReplyDeleteif (a.length() == 0){
return b;
}
if (b.length() == 0){
return a;
}
return "" + a.charAt(0) + b.charAt(0) + mixString(a.substring(1), b.substring(1));
}
public String mixString(String a, String b) {
ReplyDeletereturn (a.length() == 0)? b:
(b.length() == 0)? a:
"" + a.charAt(0) + b.charAt(0) + mixString(a.substring(1), b.substring(1));
}
public String mixString(String a, String b) {
ReplyDeleteString mix = "";
if(a.length()> b.length()){
for(int i=0;i a.length()){
for(int j=0;j <a.length(); j++){
mix = mix + a.charAt(j) + b.charAt(j);
}
mix = mix + b.substring(a.length());
} if(a.length() == b.length()){
for(int i=0;i <b.length(); i++){
mix = mix + a.charAt(i) + b.charAt(i);
}
}
return mix;
}
AAaauuuuugGgHhhhhh
ReplyDeletepublic String mixString(String a, String b) {
if(a=="") return b;
if(b=="") return a;
int len = a.length()<b.length() ? a.length() : b.length();
String res="";
for(int i=0;i<len;i++){
res+=a.substring(i,i+1)+b.substring(i,i+1);
}
if(a.length()<b.length()){
if(a.length()*2==b.length()){
res+=b.substring(b.length()-a.length());
} else {
res+=b.substring((b.length()%a.length())*a.length());
}
}
if(b.length()<a.length()){
if(b.length()*2==a.length()){
res+=a.substring(a.length()-b.length());
} else {
res+=a.substring((b.length()%a.length())*b.length());
}
}
return res;
}
public String mixString(String a, String b) {
ReplyDeleteString res = "";
int alen = a.length();
int blen = b.length();
int min = Math.min(alen, blen);
int i;
for(i = 0; i < min; ++i) {
res += a.substring(i, i+1) + b.substring(i, i+1);
}
return (i < alen) ? (res += a.substring(i, alen)) : (res += b.substring(i, blen));
}
public String mixString(String a, String b) {
ReplyDeleteString result="";
int minTall=Math.min(a.length(), b.length() );
for(int i=0;ib.length())return a;
else return b;}
public String mixString(String a, String b)
ReplyDelete{
int xlen = a.length() ;
int ylen = b.length() ;
int max = Math.max(xlen, ylen) ;
String Roomy = "" ;
for(int i = 0 ; i < max ; i++)
{
if(i <= xlen-1)
Roomy += a.substring(i,i+1);
if(i <= ylen-1)
Roomy += b.substring(i,i+1) ;
}
return Roomy ;
}
public String mixString(String a, String b)
ReplyDelete{
String s = "";
for(int i =0; i < (a.length() + b.length()); i++)
{
if(a.length() > i) s = s + a.charAt(i);
if(b.length() > i) s = s + b.charAt(i);
}
return s;
}
public String mixString(String a, String b) {
ReplyDeleteStringBuilder str = new StringBuilder();
if(a.length() > 0 && a.length() <= b.length()){
for(int i = 0; i < a.length(); i++){
str.append(a.charAt(i));
str.append(b.charAt(i));
}
str.append(b.substring(a.length(),b.length()));
}
else if(a.length() > 0 && b.length() >= b.length()){
for(int i = 0; i < b.length(); i++){
str.append(a.charAt(i));
str.append(b.charAt(i));
}
str.append(a.substring(b.length(), a.length()));
}
else return b;
return str.toString();
}
public String mixString(String a, String b) {
ReplyDeleteint longest = 0 ;
if(a.length() >= b.length()) longest = a.length();
else longest = b.length();
String newStr = "" ;
for(int x = 0; x < longest ; x++) {
if (a.length() > x) newStr = newStr + a.charAt(x) ;
if (b.length() > x) newStr = newStr + b.charAt(x) ;
}
return newStr ;
}
String x= "";
ReplyDeleteint alen = a.length();
int blen = b.length();
String sub = "";
if(alen > blen) sub += a.substring(blen);
else sub += b.substring(alen);
for (int i = 0; i < alen; i++) {
if(a.substring(i).isEmpty() || b.substring(i).isEmpty()) break;
else x += a.charAt(i) +""+ b.charAt(i);
}
return x+=sub;
public String mixString(String a, String b) {
ReplyDeleteint max=0,min=0;
String str="";
String maxx="";
if(a.length()>=b.length()){
max=a.length();
min=b.length();
maxx=a;
}
else{
max=b.length();
min=a.length();
maxx=b;
}
for(int i=0;i0){
str=str+a.charAt(i)+b.charAt(i);
min--;
}
else{
str=str+maxx.charAt(i);
}
}
return str;
}