Created
November 1, 2018 16:19
-
-
Save klg71/5c3cb51b15eb3f3afd1bd550b4f0a211 to your computer and use it in GitHub Desktop.
WordFunnel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Objects; | |
import static java.util.stream.Collectors.toList; | |
public class WordFunnel { | |
public static void main(final String... args) { | |
if (funnel("leave", "eave")) { | |
System.out.println("funnel"); | |
} | |
} | |
private static boolean funnel(final String first, final String second) { | |
final List<Integer> firstList = first.chars().boxed().collect(toList()); | |
for (int i = 0; i < firstList.size(); i++) { | |
final List<Integer> tempList = getSublist(firstList, i); | |
if (join(tempList).equals(second)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
private static List<Integer> getSublist(final List<Integer> firstList, final int i) { | |
final List<Integer> tempList = new ArrayList<>(firstList); | |
tempList.remove(i); | |
return tempList; | |
} | |
private static String join(final List<Integer> tempList) { | |
final List<String> chars = tempList.stream().map(x -> (char) x.intValue()).map(Objects::toString).collect(toList()); | |
return String.join("", chars); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.String.join | |
import java.util.* | |
import java.util.stream.Collectors.toList | |
fun main(args: Array<String>) { | |
if(funnel("leave", "eave")) { | |
System.out.println("funnel") | |
} | |
} | |
private fun funnel(first: String, second: String) = | |
first.chars().boxed().collect(toList()).run { | |
(0..size).any { | |
getSublist(it).joinChars() == second | |
} | |
} | |
private fun List<Int>.getSublist(i: Int) = | |
ArrayList<Int>(this).apply { | |
removeAt(i) | |
} | |
private fun List<Int>.joinChars() = | |
stream().map { it.toChar().toString() }.collect(toList<String>()).let { | |
join("", it) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment