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

Commonmark migration
Source Link

#Java 7, 256 bytes

Java 7, 256 bytes

import java.util.*;String c(String...a){Map s=new HashMap();int j,i=1,l=a[0].length();for(;i<a.length;i++)if((j=a[0].indexOf(a[i]))>-1)s.put(j,s.get(j)!=null?s.get(j)+" "+a[i]:a[i]);a[0]="";for(j=0;j<l;j++)a[0]+=s.get(j)!=null?s.get(j)+" ":"";return a[0];}

It should definitely be possible to golf this more by using a different approach, but this will do for now..

Ungolfed & test code:

Try it here.

import java.util.*;
class M{
 static String c(String... a){
 Map s = new HashMap();
 int j,
 i = 1,
 l = a[0].length();
 for(; i < a.length; i++){
 if((j = a[0].indexOf(a[i])) > -1){
 s.put(j, s.get(j) != null
 ? s.get(j) + " " + a[i]
 : a[i]);
 }
 }
 a[0] = "";
 for(j = 0; j < l; j++){
 a[0] += s.get(j) != null
 ? s.get(j) + " "
 : "";
 }
 return a[0];
 }
 public static void main(String[] a){
 System.out.println(c("dogcatfrog", "cat", "frog", "dog"));
 System.out.println(c("xxcatfixsxhingonxgrapexxxfishingcxat", "cat", "grape", "catfish", "fishing"));
 System.out.println(
 c("dababbabadbaccbcbaaacdacdbdd ", "aa", "bb", "cc", "dd", "ba", "ba", "ba", "ab", "ac", "da", "db", "dc"));
 System.out.println(c("flea", "antelope"));
 }
}

Output:

dog cat frog 
cat grape fishing 
da ab ba ba ba bb db ac cc aa dd 

#Java 7, 256 bytes

import java.util.*;String c(String...a){Map s=new HashMap();int j,i=1,l=a[0].length();for(;i<a.length;i++)if((j=a[0].indexOf(a[i]))>-1)s.put(j,s.get(j)!=null?s.get(j)+" "+a[i]:a[i]);a[0]="";for(j=0;j<l;j++)a[0]+=s.get(j)!=null?s.get(j)+" ":"";return a[0];}

It should definitely be possible to golf this more by using a different approach, but this will do for now..

Ungolfed & test code:

Try it here.

import java.util.*;
class M{
 static String c(String... a){
 Map s = new HashMap();
 int j,
 i = 1,
 l = a[0].length();
 for(; i < a.length; i++){
 if((j = a[0].indexOf(a[i])) > -1){
 s.put(j, s.get(j) != null
 ? s.get(j) + " " + a[i]
 : a[i]);
 }
 }
 a[0] = "";
 for(j = 0; j < l; j++){
 a[0] += s.get(j) != null
 ? s.get(j) + " "
 : "";
 }
 return a[0];
 }
 public static void main(String[] a){
 System.out.println(c("dogcatfrog", "cat", "frog", "dog"));
 System.out.println(c("xxcatfixsxhingonxgrapexxxfishingcxat", "cat", "grape", "catfish", "fishing"));
 System.out.println(
 c("dababbabadbaccbcbaaacdacdbdd ", "aa", "bb", "cc", "dd", "ba", "ba", "ba", "ab", "ac", "da", "db", "dc"));
 System.out.println(c("flea", "antelope"));
 }
}

Output:

dog cat frog 
cat grape fishing 
da ab ba ba ba bb db ac cc aa dd 

Java 7, 256 bytes

import java.util.*;String c(String...a){Map s=new HashMap();int j,i=1,l=a[0].length();for(;i<a.length;i++)if((j=a[0].indexOf(a[i]))>-1)s.put(j,s.get(j)!=null?s.get(j)+" "+a[i]:a[i]);a[0]="";for(j=0;j<l;j++)a[0]+=s.get(j)!=null?s.get(j)+" ":"";return a[0];}

It should definitely be possible to golf this more by using a different approach, but this will do for now..

Ungolfed & test code:

Try it here.

import java.util.*;
class M{
 static String c(String... a){
 Map s = new HashMap();
 int j,
 i = 1,
 l = a[0].length();
 for(; i < a.length; i++){
 if((j = a[0].indexOf(a[i])) > -1){
 s.put(j, s.get(j) != null
 ? s.get(j) + " " + a[i]
 : a[i]);
 }
 }
 a[0] = "";
 for(j = 0; j < l; j++){
 a[0] += s.get(j) != null
 ? s.get(j) + " "
 : "";
 }
 return a[0];
 }
 public static void main(String[] a){
 System.out.println(c("dogcatfrog", "cat", "frog", "dog"));
 System.out.println(c("xxcatfixsxhingonxgrapexxxfishingcxat", "cat", "grape", "catfish", "fishing"));
 System.out.println(
 c("dababbabadbaccbcbaaacdacdbdd ", "aa", "bb", "cc", "dd", "ba", "ba", "ba", "ab", "ac", "da", "db", "dc"));
 System.out.println(c("flea", "antelope"));
 }
}

Output:

dog cat frog 
cat grape fishing 
da ab ba ba ba bb db ac cc aa dd 
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394

#Java 7, 256 bytes

import java.util.*;String c(String...a){Map s=new HashMap();int j,i=1,l=a[0].length();for(;i<a.length;i++)if((j=a[0].indexOf(a[i]))>-1)s.put(j,s.get(j)!=null?s.get(j)+" "+a[i]:a[i]);a[0]="";for(j=0;j<l;j++)a[0]+=s.get(j)!=null?s.get(j)+" ":"";return a[0];}

It should definitely be possible to golf this more by using a different approach, but this will do for now..

Ungolfed & test code:

Try it here.

import java.util.*;
class M{
 static String c(String... a){
 Map s = new HashMap();
 int j,
 i = 1,
 l = a[0].length();
 for(; i < a.length; i++){
 if((j = a[0].indexOf(a[i])) > -1){
 s.put(j, s.get(j) != null
 ? s.get(j) + " " + a[i]
 : a[i]);
 }
 }
 a[0] = "";
 for(j = 0; j < l; j++){
 a[0] += s.get(j) != null
 ? s.get(j) + " "
 : "";
 }
 return a[0];
 }
 public static void main(String[] a){
 System.out.println(c("dogcatfrog", "cat", "frog", "dog"));
 System.out.println(c("xxcatfixsxhingonxgrapexxxfishingcxat", "cat", "grape", "catfish", "fishing"));
 System.out.println(
 c("dababbabadbaccbcbaaacdacdbdd ", "aa", "bb", "cc", "dd", "ba", "ba", "ba", "ab", "ac", "da", "db", "dc"));
 System.out.println(c("flea", "antelope"));
 }
}

Output:

dog cat frog 
cat grape fishing 
da ab ba ba ba bb db ac cc aa dd 

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