Golang 中 Hackerrank 缩写问题的记忆化和递归实现
所以我尝试通过使用递归和记忆来解决 Hackerrank 缩写问题。当测试用例很短时还好,但是当字符串开始变大时,我发现我的代码中途结束(根据我在 Hackkerrank 提供的调试窗口中看到的内容),所以我在 16 个测试用例中得到了 5 个错误。我不明白为什么会发生这种情况,希望有人能帮助我调试代码并告诉我我错过了什么:(。它已经被bug了两天了。顺便说一句,我的英语不好。在这里我提供了问题和我的问题代码,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论