Scala O(N^2) brute force nice solution


#1

Since complexity or efficiency isn’t the focus of this exercise why not deliver a beautifully clean and easy brute force solution ?

class Solution {
  def strStr(text: String, string: String): Int = {

    def compare(s1: String, s2: String): Boolean = {
      if (s2.isEmpty) true
      else if (s1.isEmpty) false
      else if (s1.head == s2.head) compare(s1.tail, s2.tail)
      else false
    }

    def loop(remaining: String, index: Int): Int = {
      if (remaining.isEmpty) -1
      else if (compare(remaining, string)) index
      else loop(remaining.tail, index + 1)
    }

    loop(text, 0)
  }

}