Find not containing two character strings

Hi everyone,
Would someone be able to help me understand how to approach or find the solution to this question? I’d really appreciate any guidance.
Question: Find strings that don't contain the strings ab, cd, pq, or xy, even if they are part of one of the other requirements.
Input: "zgsnvcdmlfuplrubt" "vlhagaovgqjmgvwq" "ffumlmqwfcsyqpss"
This is how I started.

 func wordsNotIncludingAbCdPqXy(_ input: String) -> Bool {
 
    let vowels: Set<String> = ["ab","cd","pq","xy"]
    print(vowels)
    var result = false

    for string in vowels {
            if !vowels.contains(input) {
                result = true
            }
        }
    return result
} 
import Algorithms

extension String {
  var containsConsecutiveLetters: Bool {
    windows(ofCount: 2).contains(
      where: (["ab", "cd", "pq", "xy"] as Set<Substring>).contains
    )
  }
}

https://swiftpackageindex.com/apple/swift-algorithms/1.2.1/documentation/algorithms/swift/collection/windows(ofcount:)

1 Like

If I understand you correctly, you want to check if the input contains a vowel. The code you have checks if the vowels set contains the whole input, which is not what you intended.

It looks like a simple logic issue, and you can even (lightly) optimize it. Here is a corrected snippet (but it can be written multiple different ways):

 static let vowels: Set<String> = ["ab","cd","pq","xy"]
 func doesNotContainVowels(_ input: String) -> Bool {
    for vowel in Self.vowels { // or use 'vowels' (replace 'Self.vowels')
            if input.contains(vowel) {
                return false
            }
        }
    return true
 }
1 Like