Skip to content

Instantly share code, notes, and snippets.

@humbertodosreis
Last active September 9, 2024 23:15
Show Gist options
  • Select an option

  • Save humbertodosreis/dd5691640f6ed02b9a1a571cd7b43048 to your computer and use it in GitHub Desktop.

Select an option

Save humbertodosreis/dd5691640f6ed02b9a1a571cd7b43048 to your computer and use it in GitHub Desktop.
Given an array of characters and another array of words, create a function that can tell which words can be formed with the characters of the array.
package com.github.humbertodosreis
/**
* Given an array of characters and another array of words, create a function that can tell which words can be formed with the characters of the array.
* characters = ['o', 'f', 'c' ,'f', 'e', 'e', 'i', 'n', 't', 'b', 'l']
* words = ['coffin' , 'coffee', 'win', 'beetle']
* output = ['coffin' , 'coffee']
*/
fun main() {
val characters = arrayOf('o', 'f', 'c', 'f', 'e', 'e', 'i', 'n', 't', 'b', 'l')
val words = arrayOf("coffin", "coffee", "win", "beetle")
println(whichWordsCanBeFormedV1(characters, words).joinToString(", "))
println(whichWordsCanBeFormedV2(characters, words).joinToString(", "))
val characters2 = arrayOf('o', 'f', 'c', 'f', 'e', 'e', 'i', 'n', 't', 'b', 'l', 'f')
val words2 = arrayOf("coffin", "coffee", "win", "beetle")
println(whichWordsCanBeFormedV1(characters2, words2).joinToString(", "))
println(whichWordsCanBeFormedV2(characters2, words2).joinToString(", "))
}
fun whichWordsCanBeFormedV1(input: Array<Char>, words: Array<String>): Array<String> {
val output = mutableListOf<String>()
val allCharsCount = input.associateWith { c -> input.count { it == c } }
for (word in words) {
val chars = word.toCharArray()
val charsCount = chars.associateWith { char -> word.count { it == char } }
var totalChars = charsCount.size
for ((char, total) in charsCount) {
if (allCharsCount.containsKey(char) && allCharsCount[char]!! >= total ) {
totalChars--
}
}
if (totalChars == 0) {
output.add(word)
}
}
return output.toTypedArray()
}
fun whichWordsCanBeFormedV2(input: Array<Char>, words: Array<String>): Array<String> {
val output = mutableListOf<String>()
val totalOfEachLetterFromInput = input.toSet().associateWith { char -> input.count { it == char } }
for (word in words) {
val totalOfEachLetter = word.toSet().associateWith { char -> input.count { it == char } }.toMutableMap()
if (totalOfEachLetter.values.any { it == 0 }) {
break
}
for ((char, total) in totalOfEachLetter) {
if (totalOfEachLetterFromInput.containsKey(char) && totalOfEachLetterFromInput[char]!! >= total) {
totalOfEachLetter[char] = 0
}
}
if (totalOfEachLetter.values.all { it == 0 }) {
output.add(word)
}
}
return output.toTypedArray()
}
import com.github.humbertodosreis.whichWordsCanBeFormedV1
import com.github.humbertodosreis.whichWordsCanBeFormedV2
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
class MainTest {
@Test
fun `Give a list of words to verify if possible to formed then return the which words can be formed`() {
val characters = arrayOf('o', 'f', 'c' ,'f', 'e', 'e', 'i', 'n', 't', 'b', 'l')
val words = arrayOf("coffin" , "coffee", "win", "beetle")
val expectOutput = arrayOf("coffin" , "coffee")
val result = whichWordsCanBeFormedV1(characters, words)
Assertions.assertArrayEquals(expectOutput, result)
}
@Test
fun `Given a list of words and a list of chars with more duplicate letter than letters in the word`() {
val characters = arrayOf('o', 'f', 'c' ,'f', 'e', 'e', 'i', 'n', 't', 'b', 'l', 'f')
val words = arrayOf("coffin" , "coffee", "win", "beetle")
val expectOutput = arrayOf("coffin" , "coffee")
Assertions.assertArrayEquals(expectOutput, whichWordsCanBeFormedV1(characters, words))
Assertions.assertArrayEquals(expectOutput, whichWordsCanBeFormedV2(characters, words))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment