简体   繁体   中英

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

I am solving a algorithm problem, and I use both Python and Swift to solve it. In python, I can use a for else syntax solve it easily. But in Swift, I am struggling to find a way that similar to python's for else syntax.

Here is the algorithm problem, it may help you understand what I am doing.

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.

Here are my two sets code.

The Python code works.

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

The swift code not works well.

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
    }
}

You could just introduce a flag to record if break is called or not. The statement

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

is the same as

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

But note that you don't need the for char in item.characters loop at all, since you could just use the Set.isDisjointWith(_:) method .

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

(On Swift 3 this method is renamed to Set.isDisjoint(with:) )

I would like to share my answer. Appreciate Kennytm's help.

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
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM