Checking if two strings are permutations of each other in Python

前端 未结 22 2458
旧时难觅i
旧时难觅i 2020-12-08 16:04

I\'m checking if two strings a and b are permutations of each other, and I\'m wondering what the ideal way to do this is in Python. From the Zen of

22条回答
  •  一向
    一向 (楼主)
    2020-12-08 16:25

    In Swift (or another languages implementation), you could look at the encoded values ( in this case Unicode) and see if they match.

    Something like:

    let string1EncodedValues = "Hello".unicodeScalars.map() {
    //each encoded value
    $0
    //Now add the values
    }.reduce(0){ total, value in
        total + value.value
    }
    
    let string2EncodedValues = "oellH".unicodeScalars.map() {
        $0
        }.reduce(0) { total, value in
        total + value.value
    }
    
    let equalStrings = string1EncodedValues == string2EncodedValues ? true : false
    

    You will need to handle spaces and cases as needed.

提交回复
热议问题