Skip to content

Welcome to Tech by Example

Menu
  • Home
  • Posts
  • System Design Questions
Menu

Kth Distinct String in an Array Program

Posted on March 14, 2022March 14, 2022 by admin

Overview

An input string array is given which can have duplicate strings as well. An input number k is also provided. Idea is to find the first kth distinct string in the given input string array

Let’s understand it with an example

Input: ["a", "c", "b" , "c", "a", "b", "e", "d"]
k=2

Output: "d"

In the above array below strings are duplicated

  • “a”
  • “c”
  • “b”

and below strings are not duplicated

  • “e”
  • “d”

Since string “d” appears second in order and k is 2 hence the output is “d”Another example

Input: ["xxx", "xx" "x"]
k=2

Output: "xx"

For the same reasoning as above

Program

Here is the program for the same.

package main

import "fmt"

func rob(nums []int) int {
	lenNums := len(nums)

	if lenNums == 0 {
		return 0
	}

	maxMoney := make([]int, lenNums)

	maxMoney[0] = nums[0]

	if lenNums > 1 {
		maxMoney[1] = nums[1]
	}

	if lenNums > 2 {
		maxMoney[2] = nums[2] + nums[0]
	}

	for i := 3; i < lenNums; i++ {
		if maxMoney[i-2] > maxMoney[i-3] {
			maxMoney[i] = nums[i] + maxMoney[i-2]
		} else {
			maxMoney[i] = nums[i] + maxMoney[i-3]
		}

	}

	max := 0
	for i := lenNums; i < lenNums; i++ {
		if maxMoney[i] > max {
			max = maxMoney[i]
		}
	}

	return max
}

func main() {
	output := rob([]int{2, 3, 4, 2})
	fmt.Println(output)

	output = rob([]int{1, 6, 8, 2, 3, 4})
	fmt.Println(output)

}

Output

6
13
©2025 Welcome to Tech by Example | Design: Newspaperly WordPress Theme