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("不是异位词")
}
}
}
}











网友评论