Swift 中是否有类似于 Python 的 for else 语法的表达式

Is there any expression in Swift that is similar to Python's for else syntax

我正在解决一个算法问题,我同时使用Python和Swift来解决它。在 python 中,我可以使用 for else 语法轻松解决它。但是在 Swift 中,我正在努力寻找一种类似于 python 的 for else 语法的方法。

这里是算法题,可以帮助你理解我在做什么。

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.

Example 1: Given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]

Return 16

The two words can be "abcw", "xtfn".

Example 2: Given ["a", "ab", "abc", "d", "cd", "bcd", "abcd"]

Return 4

The two words can be "ab", "cd".

Example 3: Given ["a", "aa", "aaa", "aaaa"]

Return 0

No such pair of words.

这是我的两组代码。

Python 代码有效。

class Solution(object):
    def maxProduct(self, words):

        maximum = 0
        while words:
            currentWord = set(words[0])
            current_length = len(words[0])
            words = words[1:]

            for ele in words:
                for char in currentWord:
                    if char in ele:
                        break
                else:
                    maximum = max(maximum,current_length*len(ele))
        return maximum

swift 代码运行不正常。

class Solution
{
    func maxProduct(words: [String]) -> Int
    {
        var input = words
        let length = input.count
        var maximum = 0
        while input.count != 0
        {
            let cur_word = Set(input[0].characters)
            let cur_length = input[0].characters.count
            input = Array(input[1..<length])


            for item in input
            {
                for char in item.characters
                {
                    if cur_word.contains(char)
                    {
                        break
                    }
                }

                // how add a control follow here? if cur_word does not share same character with item, then does the below max statement

                //else
                //{
                    maximum = max(maximum,cur_length*(item.characters.count))
                //}


            }

        }
        return maximum
    }
}

你可以引入一个标志来记录是否调用了break。声明

for a in b:
    if c(a):
        break
else:
    d()

相同
found = False
for a in b:
    if c(a):
        found = True
        break
if not found:
    d()

但是请注意,您根本不需要 for char in item.characters 循环,因为您可以只使用 Set.isDisjointWith(_:) method.

if cur_word.isDisjointWith(item.characters) {
    maximum = ...
}

(在Swift3这个方法是renamed to Set.isDisjoint(with:))

我想分享我的答案。感谢 Kennytm 的帮助。

class Solution
{
    func maxProduct(words: [String]) -> Int
    {
        var input = words
        var length = input.count
        var maximum = 0
        while input.count != 0
        {
            let cur_word = Set(input[0].characters)
            let cur_length = input[0].characters.count
            input = Array(input[1..<length])
            length -= 1

            for item in input
            {
                if cur_word.isDisjointWith(item.characters)
                {
                    maximum = max(maximum, cur_length*(item.characters.count))
                }
            }

        }
        return maximum
    }
}