我正在解决leetcode问题249.分组移位字符串.有人能解释一下密钥是如何存储在hashMap中的吗?

我们可以通过将字符串的每个字母移到其连续的字母来移位字符串.

例如,"abc"可以改为"bcd". 我们可以不断地移动弦以形成一个序列. 例如,我们可以不断移动"abc"以形成序列:"abc"->"bcd"->...->"xyz".

给出一个字符串数组,将属于同一移位序列的所有strings[i]个字符串分组成一组.你可以按任何顺序交回答案.

Input: strings = ["abc","bcd","acef","xyz","az","ba","a","z"]
Output: [["acef"],["a","z"],["abc","bcd","xyz"],["az","ba"]]
   func groupStrings(_ strings: [String]) -> [[String]] {
           
        var dict = [[Int]: [String]]()
        
           for word in strings {         
               var key = [0]
               if word.count > 1 {
                   var a = Array(word)
                   let pivot = Int(a[0].asciiValue!) - 97
               
                   for i in 1..<a.count {
                       let index = (Int(a[i].asciiValue!) - 97 + 26 - pivot) % 26
                       key.append(index)
                   }
               }
               
               if var array = dict[key] {
                   array.append(word)
                   dict[key] = array
               } else {
                   dict[key] = [word]
               }
           }
           return dict.keys.compactMap { dict[$0] }
    }
"[[0, 2, 4, 5]: ["acef"], [0, 25]: ["az", "ba"], [0]: ["a", "z"], [0, 1, 2]: ["abc", "bcd", "xyz"]]"

推荐答案

你的问题不是很清楚,但我相信你是在要求对groupStrings函数做出解释.

在你的问题中,你必须找到移位序列.例句:abc -> bcd -> cde ..... -> xyz.

因此,这背后的逻辑是判断每个字符的ASCII个值.因为如果两个字符串形成移位序列,则它们的任何连续字符对的ASCII值之间的差异是相同的.

例如,我们知道abcbcdxyz按移位顺序`.

Characters    ASCCI values     Difference from the 1st value

 a b c           97 98 99.          0 1 2
 b c d           98 99 100.         0 1 2
 x y z           120 121 122.       0 1 2

基本上,在您的函数中发生的事情是创建一个字典,其中包含keys个差异和values个字符串.

func groupStrings(_ strings: [String]) -> [[String]] {
        
     var dict = [[Int]: [String]]() // initialize the dictionary
     
        for word in strings {
            var key = [0]
            if word.count > 1 {
                var a = Array(word) //create an array from the charachters of the word
                let pivot = Int(a[0].asciiValue!) - 97 // get the ASCII value of the 1st lestter
                // we substract 97 because for english letters ASCII values start from 97.
                // by substracting 97 we can map the ASCCII of a to 0, b to 1 .... z to 26
            
                for i in 1..<a.count {
                    // calulate the differences between the ASCII values of any consecutive pairs
                    let index = (Int(a[i].asciiValue!) - 97 + 26 - pivot) % 26
                    key.append(index)
                }
            }

            // here check if there is an already a difference sequence in the dictionary
            //if true we append the word to that exsiting key value pair
            //else create a new key value pair
            if var array = dict[key] {
                array.append(word)
                dict[key] = array
            } else {
                dict[key] = [word]
            }
        }
        return dict.keys.compactMap { dict[$0] }
 }

Swift相关问答推荐

如何取消正在视图修改器中运行的任务

Swift:iVar + Equatable上的协议约束

通过SwiftUI中的列表 Select 从字典中检索值

SwiftUI - NavigationLink(value:)和navigationDestination不工作

Swift 运算符中 inout 的行为

减小SF符号的边框宽度

在表单中对齐文本框

ConfirmationDialog取消swiftui中的错误

RxSwift 事件触发了两次

您可以在运行时访问 Picker 元素并更改它们的 colored颜色 吗

将阻塞函数集成到 Swift 异步中

展开 List 中的 HStack 以端到端但不是从上到下,因此看起来项目之间有更宽的空间

我们如何显示关于 UITextField 是 xCode Swift 的建议?

swift:修改字典中的数组

我可以在 Swift 中为泛型类型 T 分配默认类型吗?

Swift 中 NSFetchRequest 的多个 NSPredicates?

Swift中switch 盒的详尽条件

'#selector' 的参数不引用 '@objc' 方法、属性或初始化程序

无法停止 AVPlayer

swift中的匿名类