【Review Diary】804. The Only Morse Codeword

Get into the habit of writing together! This is the 9th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the event details .

【Review Diary】804. The Only Morse Codeword

The twenty-ninth chapter of this review diary is titled: 804. The only Morse code word , simple

1. Topic description:

Today, Sunday, let's keep getting used to it . Today's daily question of leetcode is a Morse code word . At first glance, can we also decrypt the Morse code? feel a little interesting

On Sunday, leetcode also wants us to relax and write a more understandable topic, let's take a look

2. What idea does this question examine? What is your thinking?

What important information does this question give:

  • The string mapping relationship from letter a to letter z is given. We don’t need to pay attention to this mapping relationship, even if it becomes other mapping relationship, it doesn’t matter. The point is that we just map them one by one.
  • The title requires that given a word, we need to convert it into a corresponding string, and in the given word array, different words may have the same splicing result string, then, we need to calculate, among these result strings , different kinds of string translations

Seeing the key information given above, let us translate letters or words into corresponding strings according to the mapping relationship. There is nothing to say about this.

Write a help list directly, the letters of az correspond to the list index of 0-25, find the corresponding string according to the index, and finally splicing it together

For example this

Then we can easily spliced ​​into strings, how to find different strings ?

我们可以使用 hash 表来进行解决,hash 表的话,我们直接就可以使用 golang 中的 map 可以处理, c++ 中的 set 也可以处理,只要 key 是不会重复的就可以处理了

按照上述的思路,我们就可以很明确的得到这题的解决方式了,接下来,咱们就将上述思路,翻译成代码即可,思路还是很清晰的吧

三、编码

根据上述逻辑和分析,我们就可以翻译成如下代码,编码的时候,这里需要注意

  • 需要创建好一个帮助列表,存放,每一个字母对应的字符串
  • 使用 hash 表来处理和提取结果字符串的种类

编码如下:

func uniqueMorseRepresentations(words []string) int {
    // 先写一个帮助列表
    var helper = []string{
        ".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
        "....", "..", ".---", "-.-", ".-..", "--", "-.",
        "---", ".--.", "--.-", ".-.", "...", "-", "..-",
        "...-", ".--", "-..-", "-.--", "--..",
    }
    m := map[string]int{}
    // 遍历单词
    for _,word := range words {
        // 开始处理每一个单词
        tmp := ""
        for _,ch := range word {
            tmp += helper[ch - 'a']
        }
        m[tmp]++
    }
    return len(m)
}
复制代码

看完上述编码,还是很简单明了的,但是写过 golang 的兄弟,知道咱们直接拼接字符串效率是比较低, 我们可以使用 golang 自带的库, strings ,将上述关键代码替换成这样

tmp := &strings.Builder{}
tmp.WriteString(helper[ch-'a'])
m[trans.String()]++
复制代码

使用 strings.Builder 和 直接使用拼接字符串相比 strings.Builder 效率会高很多,具体原因是为什么,xdm 可以思考一下

四、总结:

此处,我们可以看到本题的时间复杂度是 O(S) ,此处的 S 可不是 单词的数量,而是每个单词的字符长度和,仔细看上述编码,咱们其实遍历的是每个单词的字符

此处的空间复杂度也是 O(S) ,我们创建的 tmp 字符串,开辟的空间也是如此

原题地址:804. 唯一摩尔斯密码词

今天就到这里,学习所得,若有偏差,还请斧正

欢迎点赞,关注,收藏

朋友们,你的支持和鼓励,是我坚持分享,提高质量的动力

好了,本次就到这里

技术是开放的,我们的心态,更应是开放的。拥抱变化,向阳而生,努力向前行。

我是小魔童哪吒,欢迎点赞关注收藏,下次见~

Guess you like

Origin juejin.im/post/7084814012906487839