Function to check if 2 strings are 1 or 0 elements different

Original Challenge:

There are three types of edits on strings edit, replace, delete. Given two strings, write a function to return if one string is 0 or 1 edit away from the other.


Ex: pale, ple -> true

      pales, pale -> true

      pale, bale -> true

      pale, bake -> false


This is what I have so far. I have quite a few errors and not sure why. Can someone point me in the right direction?

import UIKit

extension String where Element: Hashable {
    func difference(from s: String) -> Bool {
    let firstSet = Set(self)
    let otherSet = Set(s)
    if String(firstSet.symmetricDifference(otherSet)) where String.filter { $0 <= 1 }
    return true
    } else {
    return false


Here are the errors I'm getting:

- trailing 'where' clause for extension of  non-generic type 'String' (line 3)

- '(String) -> ((Character) throws -> Bool) throws -> String' is not convertible to 'Bool' (line 8)

- 'String' is not convertible to 'Bool' (line 8)

- Anonymous closure argument not contained in a closure (line 8)

- Expected ',' joining parts of a multi-clause condition (line 8)

- Replace ' where' with ',' (line 8)

- Consecutive declarations on a line must be separated by ';' (line 10)

- insert ';' (line 10)

- Expected declaration (line 10)


If someone could point me in the right direction or tell me why I'm getting these errors I'd appreciate it?

Powered by WPeMatico

You may also like...

Comments are closed.