Home > Net >  LeetCode 249. Group Shifted Strings
LeetCode 249. Group Shifted Strings

Time:09-26

I am solving the leetcode question 249. Group Shifted Strings. Can someone please explain how the keys are stored in the 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"]]"

CodePudding user response:

Your question is not much clear but I believe u are requesting an explanation to the groupStrings function.

In your question u have to find the shifting sequences. eg : abc -> bcd -> cde ..... -> xyz.

So the logic behind this is checking the ASCII values of each character. Because if two strings form a shifting sequence the differences between the ASCII values of any consecutive pair of their characters are the same.

For an example we know abc bcd xyz are in a shifting sequence`.

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

Basically what happening in your function is creating a dictionary which contains those differences as keys and strings as 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] }
 }

  • Related