Question: I am already using Prisma as an ORM for a Postgres DB. I need to run a simple raw query on a SQL Server DB. I do not need to use any of the ORM features like the Prisma schema, migrations, etc. Is it possible to do with Prisma? I am trying to minimize dependencies.

Answer: 
Your code can give a simple good/bad response as follows:

```
let wordOne = "battle"
let wordTwo = "table"

var letters = wordOne
var good = true
for letter in wordTwo {
  if letters.contains(letter) {
    let idx = letters.firstIndex(of:letter)
    letters.remove(at: idx!)
  } else {
    good = false
    break
  }
}
print(good ? "Good" : "Bad")

```

There's no need to sort the letters of each word. That doesn't make this approach any more efficient. I add the `var letters` just so the value can be modified as the loop runs.

---

Here's an alternate approach using `NSCountedSet`. This isn't a pure `Swift` class but is provided by Foundation.

```
let wordOne = "battle"
let wordTwo = "table"

let set1 = NSCountedSet(array: Array(wordOne))
let set2 = NSCountedSet(array: Array(wordTwo))
let extra = set2.filter { set2.count(for: $0) > set1.count(for: $0) }
print(extra.isEmpty ? "Good" : "Bad")

```

`NSCountedSet` is a subclass of `Set` (really of `NSSet` and `NSMutableSet`) that adds a count for each element in the set.

The `filter` makes sure there are enough of each letter. Anything left in `extra` means `wordTwo` had more instances of a letter than in `wordOne`.

As pointed out, using `allSatisfy`, rather than `filter`, would be more efficient (though trivial on such short words). Change the last two lines to:

```
let good = set2.allSatisfy { set2.count(for: $0) <= set1.count(for: $0) }
print(good ? "Good" : "Bad")

```
I like @HangarRash answer, but you could also use sort to your advantage, i.e. when letters are sorted you can move in both arrays simultaneously and stop as soon as the first difference was found (no need to remove anything):

```
func isContained(in word1: String, word word2: String) -> Bool {

    var word1Sorted = word1.sorted()
    var word2Sorted = word2.sorted()

    var c1 = word1Sorted.count - 1
    var c2 = word2Sorted.count - 1

    while c2 >= 0 && c1 >= 0 {

        if word1Sorted[c1] == word2Sorted[c2] {
            // Found a match - advance to next character in both arrays
            print("Valid Letter \(word2Sorted[c2])")
            c1 -= 1
            c2 -= 1
        } else if word1Sorted[c1] > word2Sorted[c2] {
            // Ignoring a letter present in wordOne, but not wordTwo
            print("Skipping Letter \(word1Sorted[c1])")
            c1 -= 1
        } else { // wordOneSorted[c1] < wordTwoSorted[c2]
            // the letter was not found in wordOneSorted - no need to continue
            print("Invalid Letter \(word2Sorted[c2])")
            return false
        }
    }

    // If we finished the loop then the result is:
    // - We've got to the beginning of word2, meaning we found all letters of it in word1
    // - Or we've got to the beginning of word1, meaning not all letters of word 2 were found
    return c2 < 0
}

```

So for example:

```
let wordOne = "battle"
let wordTwo = "table"

let good = isContained(in: wordOne, word: wordTwo)
print(good ? "Good" : "Bad")

```

will run on entire array (of the Word 2 at least):

```
Valid Letter t
Skipping Letter t
Valid Letter l
Valid Letter e
Valid Letter b
Valid Letter a
Good

```

While if there's a difference, e.g.

```
let wordOne = "battle"
let wordTwo = "tables"

let good = isContained(in: wordOne, word: wordTwo)
print(good ? "Good" : "Bad")

```

it may exit much faster:

```
Valid Letter t
Skipping Letter t
Invalid Letter s
Bad

```