/**
 * Given a string of characters and a list of words, return a new list containing only
 * the words that can be formed using the characters in the same order.
 *
 * Example:
 * filterWords("pneumonia", listOf("pea", "neon", "moon", "airplane", "nip"))
 * Output: ["pea", "neon", "moan"]
 *
 * @param characters A string of characters (can be lowercase or uppercase) representing the input
 * @param wordList A list of strings representing the list of words to filter
 * @return A new list containing only the words that can be formed using the given characters
 *
 * Note:
 * - The characters in the word do not need to be consecutive, but they must appear in the same order
 * - The solution must be case insensitive
 */
fun filterWords(characters: String, wordList: List<String>): List<String> {

    fun containsChars(characters: String, word: String): Boolean {
        var charIndex = 0
        for (char in word) {
            if (charIndex < characters.length && characters[charIndex].equals(char, ignoreCase = true)) {
                charIndex++
            }
        }
        return charIndex == characters.length
    }

    return wordList.filter { containsChars(characters, it) }
}
/**
 * Given a string of characters and a list of words, return a new list containing only
 * the words that can be formed using the characters in the same order.
 *
 * Example:
 * filterWords("pneumonia", listOf("pea", "neon", "moon", "airplane", "nip"))
 * Output: ["pea", "neon", "moan"]
 *
 * @param characters A string of characters (can be lowercase or uppercase) representing the input
 * @param wordList A list of strings representing the list of words to filter
 * @return A new list containing only the words that can be formed using the given characters
 *
 * Note:
 * - The characters in the word do not need to be consecutive, but they must appear in the same order
 * - The solution must be case insensitive
 */
fun filterWords(characters: String, wordList: List<String>): List<String> {
/**
 * Given a string of characters and a list of words, return a new list containing only
 * the words that can be formed using the characters in the same order.
 *
 * Example:
 * filterWords("pneumonia", listOf("pea", "neon", "moon", "airplane", "nip"))
 * Output: ["pea", "neon", "moan"]
 *
 * @param characters A string of characters (can be lowercase or uppercase) representing the input
 * @param wordList A list of strings representing the list of words to filter
 * @return A new list containing only the words that can be formed using the given characters
 *
 * Note:
 * - The characters in the word do not need to be consecutive, but they must appear in the same order
 * - The solution must be case insensitive
 */
fun filterWords(characters: String, wordList: List<String>): List<String> {

    fun containsChars(characters: String, word: String): Boolean {
        var charIndex = 0
        for (char in word) {
            if (charIndex < characters.length && characters[charIndex].equals(char, ignoreCase = true)) {
                charIndex++
            }
        }
        return charIndex == characters.length
    }

    return wordList.filter { containsChars(characters, it) }
}