Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

deleted 2 characters in body
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56

Java (JDK 10) Java (JDK 11), 146 bytes

String f(String x,String y){int c=x.replace(y," ").split(" ",-1).length-1;return c<1?" ."+x:y.repeat(y,c)+"."+f(y,x.replace(y,"")).replace(".","");}

Java (JDK 10), 146 bytes

String f(String x,String y){int c=x.replace(y," ").split(" ",-1).length-1;return c<1?" ."+x:repeat(y,c)+"."+f(y,x.replace(y,"")).replace(".","");}

Java (JDK 11), 146 bytes

String f(String x,String y){int c=x.replace(y," ").split(" ",-1).length-1;return c<1?" ."+x:y.repeat(c)+"."+f(y,x.replace(y,"")).replace(".","");}
added 173 characters in body
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56

Java (JDK 11)Java (JDK 10), 129146 bytes

String f(String x,String y){int c=x.splitreplace(y," ").split(" ",-1).length-1;return c<1?" ."+x:y.repeat(y,c)+"."+f(y,x.replace(y,"")).replace(".","");}

Try it online using Java 10! Try it online using Java 10! This TIO emulates String::repeat of Java 11 by writing Kevin Cruijssen's implementation as a helper method with the same amount of bytes.

String f(String x,String y){ // 
 int c=x.splitreplace(y," ").split(" ",-1).length-1; // Count the number of occurrences of y in x.
  // Countreplace(y," ") fixes the numberissue ofon occurrencesspecial ofcharacters yfor inthe x.split hereafter
 // .split(y" ",-1) makes sure that there are trailing elements
 return c<1
 ? // If there are no occurrence, return 
 " ."+x // " ."+x
 : // Else return 
 y.repeat(c) // y, c times
 + "." // append a dot
 + f(y,x.replace(y,"")).replace(".",""); // y divided by x without y, and remove all dots
}

Java (JDK 11), 129 bytes

String f(String x,String y){int c=x.split(y,-1).length-1;return c<1?" ."+x:y.repeat(c)+"."+f(y,x.replace(y,"")).replace(".","");}

Try it online using Java 10! This TIO emulates String::repeat of Java 11 by writing Kevin Cruijssen's implementation as a helper method with the same amount of bytes.

String f(String x,String y){ // 
 int c=x.split(y,-1).length-1; // Count the number of occurrences of y in x.
 // .split(y,-1) makes sure that there are trailing elements
 return c<1
 ? // If there are no occurrence, return 
 " ."+x // " ."+x
 : // Else return 
 y.repeat(c) // y, c times
 + "." // append a dot
 + f(y,x.replace(y,"")).replace(".",""); // y divided by x without y, and remove all dots
}

Java (JDK 10), 146 bytes

String f(String x,String y){int c=x.replace(y," ").split(" ",-1).length-1;return c<1?" ."+x:repeat(y,c)+"."+f(y,x.replace(y,"")).replace(".","");}

Try it online using Java 10! This TIO emulates String::repeat of Java 11 by writing Kevin Cruijssen's implementation as a helper method with the same amount of bytes.

String f(String x,String y){ // 
 int c=x.replace(y," ").split(" ",-1).length-1; // Count the number of occurrences of y in x.
  // replace(y," ") fixes the issue on special characters for the split hereafter
 // .split(" ",-1) makes sure that there are trailing elements
 return c<1
 ? // If there are no occurrence, return 
 " ."+x // " ."+x
 : // Else return 
 y.repeat(c) // y, c times
 + "." // append a dot
 + f(y,x.replace(y,"")).replace(".",""); // y divided by x without y, and remove all dots
}
Fixed TIO version (wasn't compiling due to two errors)
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394

Java (JDK 11), 129 bytes

String f(String x,String y){int c=x.split(y,-1).length-1;return c<1?" ."+x:y.repeat(c)+"."+f(y,x.replace(y,"")).replace(".","");}

Try it online using Java 10! Try it online using Java 10! This TIO emulates String::repeat of Java 11 by writing Kevin Cruijssen's implementation as a helper method with the same amount of bytes.

Explanations

String f(String x,String y){ // 
 int c=x.split(y,-1).length-1; // Count the number of occurrences of y in x.
 // .split(y,-1) makes sure that there are trailing elements
 return c<1
 ? // If there are no occurrence, return 
 " ."+x // " ."+x
 : // Else return 
 y.repeat(c) // y, c times
 + "." // append a dot
 + f(y,x.replace(y,"")).replace(".",""); // y divided by x without y, and remove all dots
}

String.repeat(int) only exists since Java 11, hence the JDK 11 header. Alas and to my knowledge, there are currently no Java 11 online compiler / tester...

Java (JDK 11), 129 bytes

String f(String x,String y){int c=x.split(y,-1).length-1;return c<1?" ."+x:y.repeat(c)+"."+f(y,x.replace(y,"")).replace(".","");}

Try it online using Java 10! This TIO emulates String::repeat of Java 11 by writing Kevin Cruijssen's implementation as a helper method with the same amount of bytes.

Explanations

String f(String x,String y){ // 
 int c=x.split(y,-1).length-1; // Count the number of occurrences of y in x.
 // .split(y,-1) makes sure that there are trailing elements
 return c<1
 ? // If there are no occurrence, return 
 " ."+x // " ."+x
 : // Else return 
 y.repeat(c) // y, c times
 + "." // append a dot
 + f(y,x.replace(y,"")).replace(".",""); // y divided by x without y, and remove all dots
}

String.repeat(int) only exists since Java 11, hence the JDK 11 header. Alas and to my knowledge, there are currently no Java 11 online compiler / tester...

Java (JDK 11), 129 bytes

String f(String x,String y){int c=x.split(y,-1).length-1;return c<1?" ."+x:y.repeat(c)+"."+f(y,x.replace(y,"")).replace(".","");}

Try it online using Java 10! This TIO emulates String::repeat of Java 11 by writing Kevin Cruijssen's implementation as a helper method with the same amount of bytes.

Explanations

String f(String x,String y){ // 
 int c=x.split(y,-1).length-1; // Count the number of occurrences of y in x.
 // .split(y,-1) makes sure that there are trailing elements
 return c<1
 ? // If there are no occurrence, return 
 " ."+x // " ."+x
 : // Else return 
 y.repeat(c) // y, c times
 + "." // append a dot
 + f(y,x.replace(y,"")).replace(".",""); // y divided by x without y, and remove all dots
}

String.repeat(int) only exists since Java 11, hence the JDK 11 header. Alas and to my knowledge, there are currently no Java 11 online compiler / tester...

added 813 characters in body
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56
Loading
added 756 characters in body
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56
Loading
added 918 characters in body
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56
Loading
added 11 characters in body
Source Link
Mr. Xcoder
  • 42.9k
  • 9
  • 87
  • 221
Loading
Source Link
Olivier Grégoire
  • 14.5k
  • 3
  • 33
  • 56
Loading

AltStyle によって変換されたページ (->オリジナル) /