kotlin coding test course, string search

Topic: String Searching

String-related problems are frequently presented in coding tests. In this course, we will address string searching problems and write algorithms and code to solve them.

Problem Description

Let’s solve the following problem:

Problem: Count the occurrences of a specific word in a given string

Given a string s and a word word, write a function that counts and returns the number of times the word appears in the string. Note that case is ignored, and the word is recognized only as a separate entity separated by spaces.

Input Example

s = "Kotlin is a programming language. kotlin is a functional programming language."
word = "kotlin"

Output Example

2

Problem Solving Process

This problem is a basic one dealing with strings, requiring simple string manipulation skills. The solution process is as follows:

  1. String normalization: Convert the entire input string to lowercase to ignore case.
  2. Word separation: Split the string based on spaces to create a list of words.
  3. Word comparison: Count and return the number of words in the generated word list that match the given word.

Code Implementation

Now let’s implement the above algorithm in Kotlin:

fun countWordOccurrence(s: String, word: String): Int {
    // 1. Convert the string to lowercase
    val normalizedString = s.toLowerCase()
    // 2. Separate the string based on spaces
    val words = normalizedString.split(" ")
    // 3. Count of the same word
    return words.count { it == word.toLowerCase() }
}

// Example usage
fun main() {
    val s = "Kotlin is a programming language. kotlin is a functional programming language."
    val word = "kotlin"
    val occurrenceCount = countWordOccurrence(s, word)
    println(occurrenceCount) // Output: 2
}

Code Explanation

The above code functions as follows:

  • Lowercase conversion: It uses the s.toLowerCase() method to convert the given string to lowercase.
  • Word separation: It uses the split(" ") method to split the string based on spaces, generating a list of words.
  • Word count: It calculates and returns the number of occurrences of the specific word in the list using count { it == word.toLowerCase() }.

Test Cases

Now let’s write some test cases to verify that the code is functioning correctly.

fun runTests() {
    val testCases = listOf(
        Pair("Kotlin is a programming language. kotlin is a functional programming language.", "kotlin") to 2,
        Pair("This is a test string. The test is important.", "test") to 2,
        Pair("Kotlin and Java are different languages. JAVA is an object-oriented language.", "java") to 2,
        Pair("String searching problem", "none") to 0
    )

    for ((input, expected) in testCases) {
        val (s, word) = input
        val result = countWordOccurrence(s, word)
        println("Input: \"$s\", Word: \"$word\", Expected Result: $expected, Actual Result: $result")
    }
}

// Run tests
runTests()

Conclusion

In this lesson, we learned how to handle strings simply in Kotlin through a string searching problem. String exploration and manipulation are fundamentals of problem-solving and are frequently encountered in real coding tests. Apply the above code in various scenarios and challenge yourself with other string-related problems!

This has been the Kotlin coding test tutorial. Thank you!