Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Program for Longest Word in Dictionary through Deleting in Go (Golang)

Posted on September 30, 2023September 30, 2023 by admin

Table of Contents

  • Overview
  • Program

Overview

A string and a dictionary of words are given. The objective is to find the longest word in the dictionarywhich is present as a subsequence in the given string. If the number of possible results is more than 1 then return the longest word with the smallest lexicographical order.

Example 1

s = "mbacnago", dictionary = ["ale","mango","monkey","plea"]
Output: "mango"

Example 2

s = "mbacnago", dictionary = ["ba","ag"]
Output: "ag"

Program

Below is the program for the same

package main
import (
"fmt"
"sort"
)
func findLongestWord(s string, dictionary []string) string {
sort.Slice(dictionary, func(i, j int) bool {
lenI := len(dictionary[i])
lenJ := len(dictionary[j])
if lenI == lenJ {
return dictionary[i] < dictionary[j]
}
return lenI > lenJ
})
lenS := len(s)
for i := 0; i < len(dictionary); i++ {
if isSubstring(s, dictionary[i], lenS) {
return dictionary[i]
}
}
return ""
}
func isSubstring(s string, sub string, lenS int) bool {
lenSub := len(sub)
if lenSub == 0 {
return true
}
if lenSub > lenS {
return false
}
for i, j := 0, 0; i < lenS && j < lenSub; {
if i+lenSub-j-1 >= lenS {
return false
}
if s[i] == sub[j] {
j++
}
if j == lenSub {
return true
}
i++
}
return false
}
func main() {
output := findLongestWord("mbacnago", []string{"ale", "mango", "monkey", "plea"})
fmt.Println(output)
output = findLongestWord("mbacnago", []string{"ba", "ag"})
fmt.Println(output)
}

Output

mango
ag

Note: Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang – Golang Advance Tutorial

Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you – All Design Patterns Golang

Also, check out our system design tutorial series here – System Design Tutorial Series

  • go
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP