Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package longestconsecutivesequence

import "fmt"

func buildSet(nums []int) map[int]bool {
set := make(map[int]bool)
for _, val := range nums {
set[val] = true
}
return set
}

func calculateConsecutiveSequence(value int, set map[int]bool) int {
length := 1
currVal := value
for {
currVal += 1
_, ok := set[currVal]
if ok {
length += 1
} else {
break
}
}
return length
}

func longestConsecutive(nums []int) int {
maxConsecutiveLength := 0
set := buildSet(nums)

for i := 0; i < len(nums); i++ {
_, ok := set[nums[i]-1]
if !ok {
fmt.Println("Left is not present => start of a sequence")
currLongestConsecutiveLength := calculateConsecutiveSequence(nums[i], set)
maxConsecutiveLength = max(maxConsecutiveLength, currLongestConsecutiveLength)
}
}
return maxConsecutiveLength
}

func max(i int, j int) int {
if i > j {
return i
}
return j
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package longestconsecutivesequence

import "testing"

func Test_longestConsecutive(t *testing.T) {
type args struct {
nums []int
}
tests := []struct {
name string
args args
want int
}{
{
name: "case1",
args: args{
nums: []int{100, 4, 200, 1, 3, 2},
},
want: 4,
},
{
name: "case2",
args: args{
nums: []int{0, 3, 7, 2, 5, 8, 4, 6, 0, 1},
},
want: 9,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := longestConsecutive(tt.args.nums); got != tt.want {
t.Errorf("longestConsecutive() = %v, want %v", got, tt.want)
}
})
}
}
15 changes: 15 additions & 0 deletions go/ArraysAndHashing/217_ContainsDuplicate/217_ContainsDuplicate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package containsDuplicate

func containsDuplicate(nums []int) bool {
hashmap := make(map[int]int8)

for _, val := range nums {
_, ok := hashmap[val]
if ok {
return true
} else {
hashmap[val] = 1
}
}
return false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package containsDuplicate

import "testing"

func Test_containsDuplicate(t *testing.T) {
type args struct {
nums []int
}
tests := []struct {
name string
args args
want bool
}{
{
name: "case1",
args: args{
nums: []int{1, 2, 3, 3},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := containsDuplicate(tt.args.nums); got != tt.want {
t.Errorf("containsDuplicate() = %v, want %v", got, tt.want)
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package productExceptSelf

func getPrefixProduct(nums []int) []int {
prefixProductArr := make([]int, len(nums))
prefixProduct := 1
for i := range nums {
prefixProductArr[i] = prefixProduct * nums[i]
prefixProduct = prefixProductArr[i]
}
return prefixProductArr
}

func getPostFixProduct(nums []int) []int {
postFixProductArr := make([]int, len(nums))
postFixProduct := 1
i := len(nums) - 1
for i >= 0 {
postFixProductArr[i] = postFixProduct * nums[i]
postFixProduct = postFixProductArr[i]
i--
}
return postFixProductArr
}

func productExceptSelf(nums []int) []int {
result := make([]int, len(nums))
prefixProductArr := getPrefixProduct(nums)
postFixProductArr := getPostFixProduct(nums)
prefixValue := 1
postFixValue := 1

for i := range result {
if i == 0 {
prefixValue = 1
} else {
prefixValue = prefixProductArr[i-1]
}
if i < len(result)-1 {
postFixValue = postFixProductArr[i+1]
} else {
postFixValue = 1
}

result[i] = prefixValue * postFixValue
}
return result
}

// For O(1) space solution we need to store the prefix Array as
// [1, nums[0], nums[1]*nums[0], nums[2]*nums[1]*nums[0] ..... ]
// then we write a loop
// Initial postfix value will be 1
// for i >=0 {
// if i == len(nums) - 1 {
// postFix = 1
// }else {
// postFix *= nums[i+1]
// }
// result[i] = postFix * prefixArray[i]
// }

func productExceptSelfOptimised(nums []int) []int {
res := make([]int, len(nums))

prefix := 1
res[0] = 1
for i := 0; i < len(nums); i++ {
res[i] = prefix

prefix *= nums[i]
}

postfix := 1
for i := len(nums) - 1; i >= 0; i-- {
res[i] *= postfix

postfix *= nums[i]
}

return res
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package productExceptSelf

import (
"reflect"
"testing"
)

func Test_productExceptSelf(t *testing.T) {
type args struct {
nums []int
}
tests := []struct {
name string
args args
want []int
}{
{
name: "case1",
args: args{
[]int{1, 2, 3, 4},
},
want: []int{24, 12, 8, 6},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := productExceptSelf(tt.args.nums); !reflect.DeepEqual(got, tt.want) {
t.Errorf("productExceptSelf() = %v, want %v", got, tt.want)
}
})
}
}

func Test_productExceptSelfOptimised(t *testing.T) {
type args struct {
nums []int
}
tests := []struct {
name string
args args
want []int
}{
{
name: "case1",
args: args{
[]int{1, 2, 3, 4},
},
want: []int{24, 12, 8, 6},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := productExceptSelfOptimised(tt.args.nums); !reflect.DeepEqual(got, tt.want) {
t.Errorf("productExceptSelfOptimised() = %v, want %v", got, tt.want)
}
})
}
}
31 changes: 31 additions & 0 deletions go/ArraysAndHashing/242_Valid_Anagram/242_Valid_Anagram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package isAnagram

func isAnagram(s string, t string) bool {
characters := make(map[byte]int)

if len(s) != len(t) {
return false
}

for i := 0; i < len(s); i++ {
_, ok := characters[s[i]]
if !ok {
characters[s[i]] = 1
} else {
characters[s[i]] += 1
}

}

for i := 0; i < len(t); i++ {
val, ok := characters[t[i]]
if ok && val > 0 {
characters[t[i]] -= 1
} else {
return false
}
}

return true

}
31 changes: 31 additions & 0 deletions go/ArraysAndHashing/242_Valid_Anagram/242_Valid_Anagram_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package isAnagram

import "testing"

func Test_isAnagram(t *testing.T) {
type args struct {
s string
t string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "case1",
args: args{
s: "anagram",
t: "nagaram",
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isAnagram(tt.args.s, tt.args.t); got != tt.want {
t.Errorf("isAnagram() = %v, want %v", got, tt.want)
}
})
}
}
33 changes: 33 additions & 0 deletions go/ArraysAndHashing/271_encode_and_decode/271_encode_and_decode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package encodeanddecode

import (
"fmt"
"strconv"
"strings"
)

func encode(strs []string) string {
delimiter := "#"
for i := 0; i < len(strs); i++ {
strs[i] = strconv.Itoa(len(strs[i])) + delimiter + strs[i]
}
return strings.Join(strs, "")
}

func decode(str string) []string {
var result []string
i := 0

for i < len(str) {
j := i
for str[j] != '#' {
j += 1
}
lengthOfString, _ := strconv.Atoi(str[i:j])
fmt.Println(i, lengthOfString)
result = append(result, str[j+1:j+1+lengthOfString])
i = j + 1 + lengthOfString
fmt.Println(i)
}
return result
}
Loading