From dbd72eb1000e19ed4595b1432e8ab4a21fa7da67 Mon Sep 17 00:00:00 2001 From: Billy Zaelani Malik Date: Wed, 13 Dec 2023 11:53:20 +0700 Subject: [PATCH] geeksforgeeks: add lexicographically next string --- .../lexicographically-next-string/README.md | 15 ++++++++++ .../lexicographically-next-string/solution.go | 23 +++++++++++++++ .../solution_test.go | 29 +++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 geeksforgeeks/lexicographically-next-string/README.md create mode 100644 geeksforgeeks/lexicographically-next-string/solution.go create mode 100644 geeksforgeeks/lexicographically-next-string/solution_test.go diff --git a/geeksforgeeks/lexicographically-next-string/README.md b/geeksforgeeks/lexicographically-next-string/README.md new file mode 100644 index 0000000..bb22dde --- /dev/null +++ b/geeksforgeeks/lexicographically-next-string/README.md @@ -0,0 +1,15 @@ +# Lexicographically next string + +Given a string, find lexicographically next string. +If string is empty, we return ‘a’. +If string contains all characters as ‘z’, we append ‘a’ at the end. +Otherwise we find first character from end which is not z and increment it. + +## Copyright Notice + +This problem is based on [content](https://www.geeksforgeeks.org/lexicographically-next-string/) +from [GeeksforGeeks](https://www.geeksforgeeks.org) +written by GeeksforGeeks +and subject to [GeeksforGeeks copyright](https://www.geeksforgeeks.org/legal/copyright-information/). +The original content from GeeksforGeeks and any modifications made here are attributed to GeeksforGeeks contributors, +and this work is shared under [CC BY-SA 4.0](../LICENSE). diff --git a/geeksforgeeks/lexicographically-next-string/solution.go b/geeksforgeeks/lexicographically-next-string/solution.go new file mode 100644 index 0000000..ca6ce26 --- /dev/null +++ b/geeksforgeeks/lexicographically-next-string/solution.go @@ -0,0 +1,23 @@ +package lexicographically_next_string + +import "strings" + +func Solution(s string) string { + i := len(s) - 1 + for i >= 0 && s[i] == 'z' { + i-- + } + + if i == -1 { + return s + "a" + } + + j := 0 + return strings.Map(func(r rune) rune { + if j == i { + r += 1 + } + j++ + return r + }, s) +} diff --git a/geeksforgeeks/lexicographically-next-string/solution_test.go b/geeksforgeeks/lexicographically-next-string/solution_test.go new file mode 100644 index 0000000..9207ac4 --- /dev/null +++ b/geeksforgeeks/lexicographically-next-string/solution_test.go @@ -0,0 +1,29 @@ +package lexicographically_next_string_test + +import ( + "testing" + + sut "github.com/minizilla/minmax/geeksforgeeks/lexicographically-next-string" + "github.com/minizilla/testr" +) + +func TestLexicographicallyNextString(t *testing.T) { + tests := map[string]struct { + in, out string + }{ + "empty": {"", "a"}, + "a": {"a", "b"}, + "z": {"z", "za"}, + "ab": {"ab", "ac"}, + "az": {"az", "bz"}, + "zz": {"zz", "zza"}, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + assert := testr.New(t) + out := sut.Solution(tc.in) + assert.Equal(out, tc.out) + }) + } +}