前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >golang刷leetcode 字符串(5)添加与搜索单词 - 数据结构设计

golang刷leetcode 字符串(5)添加与搜索单词 - 数据结构设计

作者头像
golangLeetcode
发布2022-08-02 17:31:34
1510
发布2022-08-02 17:31:34
举报
文章被收录于专栏:golang算法架构leetcode技术php

设计一个支持以下两种操作的数据结构:

void addWord(word)

bool search(word)

search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z 。. 可以表示任何一个字母。

示例:

代码语言:javascript
复制


addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true

说明:

你可以假设所有单词都是由小写字母 a-z 组成的。

解题思路

1,把加入的单词保存在字典树中。

2,插入时,

A,非通配符,在对应孩子节点插入

B,遇到.'对每个子树递归插入

3查找时,

A,在字典树中逐层匹配字符。

B,非通配符 '.' 在对应子树匹配剩下的字符。

C,遇到'.'对每个子树递归匹配,寻找一个匹配的子树。

注意事项

1,用isWord表示是否叶子节点

2,插入的时候对于每个孩子节点要判断下是否已经插入

代码语言:javascript
复制
type WordDictionary struct {
    isWord bool
    childern [26]*WordDictionary
}


/** Initialize your data structure here. */
func Constructor() WordDictionary {
    return WordDictionary{
    }
}


/** Adds a word into the data structure. */
func (this *WordDictionary) AddWord(word string)  {
    if word==""{
        return
    }
   
    if word[0]=='.'{
       for i:=0;i<26;i++{
           var t *WordDictionary
           if  this.childern[i]==nil{
           t=&WordDictionary{}
           this.childern[i]=t
           }else{
                t=this.childern[i]
           }

               if len(word)==1{
                  t.isWord=true
               }else{
                    t.AddWord(word[1:])
               }

       }
     return
    }

     var t *WordDictionary
    if this.childern[word[0]-'a']==nil{
    t=&WordDictionary{}
    this.childern[word[0]-'a']=t
    }else{
        t=this.childern[word[0]-'a']
    }
    if len(word)==1{
        t.isWord=true
         fmt.Println(*this.childern[word[0]-'a'])
        return
    }
     t.AddWord(word[1:])
     return
}


/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
func (this *WordDictionary) Search(word string) bool {
    if word==""{
        return false
    }
    if len(word)==1{
        if word[0]=='.'{
            for i:=0;i<26;i++{
                if this.childern[i]!=nil && this.childern[i].isWord{
                    return true
                }
            }
            return false
        }
        fmt.Println(word)
        return this.childern[word[0]-'a']!=nil  && this.childern[word[0]-'a'].isWord
    }
   
    if word[0]=='.'{
            for i:=0;i<26;i++{
                if this.childern[i]!=nil && this.childern[i].Search(word[1:]){
                    return true
                }
            }
            return false
    }

    t:=this.childern[word[0]-'a']
    if t==nil{
        return false
    }
    return t.Search(word[1:])
}


/**
 * Your WordDictionary object will be instantiated and called as such:
 * obj := Constructor();
 * obj.AddWord(word);
 * param_2 := obj.Search(word);
 */
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-12-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 golang算法架构leetcode技术php 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档