-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
geeksforgeeks: add lexicographically next string
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
29 changes: 29 additions & 0 deletions
29
geeksforgeeks/lexicographically-next-string/solution_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
} | ||
} |