Golang 中 Hackerrank 缩写问题的记忆化和递归实现

发布于 2025-01-10 02:00:16 字数 1780 浏览 1 评论 0原文

所以我尝试通过使用递归和记忆来解决 Hackerrank 缩写问题。当测试用例很短时还好,但是当字符串开始变大时,我发现我的代码中途结束(根据我在 Hackkerrank 提供的调试窗口中看到的内容),所以我在 16 个测试用例中得到了 5 个错误。我不明白为什么会发生这种情况,希望有人能帮助我调试代码并告诉我我错过了什么:(。它已经被bug了两天了。顺便说一句,我的英语不好。在这里我提供了问题和我的问题代码,

缩写问题 Hackerrank

func abbreviation(a string, b string) string {
    // Write your code here
    memoization := make(map[string]bool)
    result := rec(a,b,memoization)
    if result {
        return "YES"
    }else{
        return "NO"
    }
}

func rec(a string, b string, memoization map[string]bool) bool {
    fmt.Println("a=", a,"b=",b)
    baseB := ""
    var result bool
    //check
    if len(a)<len(b){//check_len
        return false
    }
    if _,ok:=memoization[a+"#"+b]; ok{//check_memoization
        return memoization[a+"#"+b]
    }
    if b==baseB{//check_leaf
        for i:=0; i<len(a); i++{
            if strings.ToUpper(string(a[0]))==string(a[0]){
                return false
            }
        }
        return true
    }
    //recursion
    if strings.ToUpper(string(a[0])) == string(a[0]){
        if b[0]==a[0]{
            a1 := a[1:]
            b1 := b[1:]
            result = rec(a1,b1,memoization)
        }else{
            return false 
        }
    }else{
        a1 := a[1:]
        a2 := strings.ToUpper(string(a[0])) + a[1:]
        if strings.ToUpper(string(a[0])) == string(b[0]){
        recursive1 := rec(a1,b,memoization)
        recursive2 := rec(a2,b,memoization)
        result = recursive1 || recursive2
        }else{
        recursive1 := rec(a1,b,memoization)
        recursive2 := false
        result = recursive1 || recursive2
        }
    }
    memoization[a+"#"+b]=result
    return result 
}

So I was attempting to do the Hackerrank Abbreviation problem by using recursion and memoization. It was fine when the test case is short but when the string started to get large, I found that my code ends halfway (according to what i saw in the debug window that Hackkerrank provides) so i got 5 of 16 test cases wrong. I couldn't understand why it happened, hope somebody could help me debugging the code and tell me what did I miss :(. It has been bugging for two days now. Sorry for my bad english btw. Here i provide the question and my code,

Abbreviation Problem Hackerrank

func abbreviation(a string, b string) string {
    // Write your code here
    memoization := make(map[string]bool)
    result := rec(a,b,memoization)
    if result {
        return "YES"
    }else{
        return "NO"
    }
}

func rec(a string, b string, memoization map[string]bool) bool {
    fmt.Println("a=", a,"b=",b)
    baseB := ""
    var result bool
    //check
    if len(a)<len(b){//check_len
        return false
    }
    if _,ok:=memoization[a+"#"+b]; ok{//check_memoization
        return memoization[a+"#"+b]
    }
    if b==baseB{//check_leaf
        for i:=0; i<len(a); i++{
            if strings.ToUpper(string(a[0]))==string(a[0]){
                return false
            }
        }
        return true
    }
    //recursion
    if strings.ToUpper(string(a[0])) == string(a[0]){
        if b[0]==a[0]{
            a1 := a[1:]
            b1 := b[1:]
            result = rec(a1,b1,memoization)
        }else{
            return false 
        }
    }else{
        a1 := a[1:]
        a2 := strings.ToUpper(string(a[0])) + a[1:]
        if strings.ToUpper(string(a[0])) == string(b[0]){
        recursive1 := rec(a1,b,memoization)
        recursive2 := rec(a2,b,memoization)
        result = recursive1 || recursive2
        }else{
        recursive1 := rec(a1,b,memoization)
        recursive2 := false
        result = recursive1 || recursive2
        }
    }
    memoization[a+"#"+b]=result
    return result 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文