【问题标题】:LeetCode 249. Group Shifted StringsLeetCode 249. 分组移位字符串
【发布时间】:2022-09-23 14:03:33
【问题描述】:

我正在解决 leetcode 问题 249. Group Shifted Strings。有人可以解释一下密钥是如何存储在 hashMap 中的吗?

We can shift a string by shifting each of its letters to its successive letter.

For example, \"abc\" can be shifted to be \"bcd\".
We can keep shifting the string to form a sequence.

For example, we can keep shifting \"abc\" to form the sequence: \"abc\" -> \"bcd\" -> ... -> \"xyz\".
Given an array of strings strings, group all strings[i] that belong to the same shifting sequence. You may return the answer in any order.

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\"]]\"

  • 你在寻找 LeetCode 问题的解释吗?如果是这样,那么 SO 与 LeetCode 有什么关系?还是问题是别的?

标签: swift string hashmap


【解决方案1】:

您的问题不太清楚,但我相信您要求对 groupStrings 函数进行解释。

在您的问题中,您必须找到转换序列。例如:abc -&gt; bcd -&gt; cde ..... -&gt; xyz

所以这背后的逻辑是检查每个字符的ASCII 值。因为如果两个字符串形成一个移位序列,则任何连续字符对的 ASCII 值之间的差异都是相同的。

例如,我们知道 abc bcd xyz 处于移位序列中。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-18
    • 2021-08-07
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 2016-07-22
    • 2021-10-10
    相关资源
    最近更新 更多