美文网首页
Swift --- LeetCode第242题,异位词

Swift --- LeetCode第242题,异位词

作者: BabyNeedCare | 来源:发表于2021-11-23 15:40 被阅读0次

LeetCode有一题,如下图:

image.png

思考了下,给出如下代码, 有其他想法的欢迎提意见交流


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        judeTwoString(first: "anagram", second: "nagaram")
        
        print("______")
    
        judeTwoString(first: "rat", second: "tra")
    }


    func judeTwoString(first: String, second: String) {

        if first.count != second.count {

            print("不是异位词")

        } else {
            
            if first.sorted(by: { $0 < $1 }) == second.sorted(by: { $0 < $1 }) {
                
                print("是异位词")
            } else {
                
                print("不是异位词")
            }
        }
    }


}

相关文章

网友评论

      本文标题:Swift --- LeetCode第242题,异位词

      本文链接:https://www.haomeiwen.com/subject/oshbtrtx.html