Last active
October 30, 2017 01:56
-
-
Save atonamy/bdea766df426347b7293f3f32514f7a2 to your computer and use it in GitHub Desktop.
Trying to solve this problem by iteration https://www.facebook.com/Engineering/videos/10152735777427200/
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
package NearbyWords | |
import java.util.LinkedList | |
import kotlin.collections.HashSet | |
typealias CharSet = MutableFifthly<Set<Char>, Iterator<Char>, Char?, Long, Long> | |
class MutableFifthly<A, B, C, D, E>( | |
var first: A, | |
var second: B, | |
var third: C, | |
var fourth: D, | |
var fifth: E | |
) | |
fun main(args : Array<String>) { | |
println(nearbyWords("gi")) | |
} | |
fun nearbyWords(word: String): Set<String> { | |
val permutations = getAllWordPermutations(word) | |
println(permutations.size) | |
return permutations.filter { isWord(it) }.toSet() | |
} | |
fun getAllWordPermutations(word: String): Set<String> { | |
val result = HashSet<String>() | |
val all_nearby_chars = getAllNearByChars(word) | |
var traversing = true | |
while(traversing) { | |
val all_nearby_chars_iter = all_nearby_chars.iterator() | |
val construct_word = StringBuilder() | |
var first: CharSet? = null | |
var previous: CharSet? = null | |
while(all_nearby_chars_iter.hasNext()) { | |
val nearbychars = all_nearby_chars_iter.next() | |
if(first == null) | |
first = nearbychars | |
if((!all_nearby_chars_iter.hasNext() || nearbychars.fourth == 0L)) { | |
nearbychars.fourth = nearbychars.fifth | |
nearbychars.third = nearbychars.second.next() | |
construct_word.append(nearbychars.third.toString()) | |
if(!nearbychars.second.hasNext()) { | |
if(!all_nearby_chars_iter.hasNext() && !first.second.hasNext() && first.fourth == 0L) | |
traversing = false | |
else if (previous != null) | |
nearbychars.second = nearbychars.first.iterator() | |
} | |
} | |
else if(nearbychars.third != null) | |
construct_word.append(nearbychars.third.toString()) | |
if(nearbychars.fourth > 0) | |
nearbychars.fourth-- | |
previous = nearbychars | |
} | |
result.add(construct_word.toString()) | |
} | |
return result | |
} | |
fun getAllNearByChars(word: String): List<CharSet> { | |
val result = LinkedList<CharSet>() | |
val sets: Array<CharSet?> = Array(word.length, { | |
null | |
}) | |
var index = 0 | |
word.forEach { | |
val nearbychars = getNearbyChar(it) | |
for(i in 0 until index) { | |
sets[i]!!.fourth = 0 | |
sets[i]!!.fifth = if (sets[i]!!.fifth == 0L) nearbychars.size.toLong() else sets[i]!!.fifth * nearbychars.size | |
} | |
val set = CharSet(nearbychars, nearbychars.iterator(), null, 0, 0) | |
sets[index++] = set | |
result.add(set) | |
} | |
return result | |
} | |
//simulating helper function | |
fun getNearbyChar(char: Char): Set<Char> { | |
return HashSet<Char>().apply { | |
when(char) { | |
'g' -> addAll(arrayListOf('g', 'h', 'f')) | |
'i' -> addAll(arrayListOf('i', 'o', 'k')) | |
} | |
} | |
} | |
//simulating helper function | |
fun isWord(word: String) : Boolean { | |
return (word == "go" || word == "hi") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment