Go Strings Package Implementation Challenge
This challenge tasks you with reimplementing a subset of Go's standard strings package. You'll focus on core string manipulation functions, gaining a deeper understanding of how these fundamental operations work under the hood. This exercise is crucial for any Go developer, as mastering string manipulation is key to building robust and efficient applications.
Problem Description
Your goal is to create a Go package named mystrings that provides implementations for the following functions, mirroring the behavior of their counterparts in the standard strings package:
mystrings.Contains(s, substr string) bool: Reports whethersubstris withins.mystrings.Count(s, substr string) int: Counts the number of non-overlapping instances ofsubstrins.mystrings.HasPrefix(s, prefix string) bool: Reports whether the stringsbegins withprefix.mystrings.HasSuffix(s, suffix string) bool: Reports whether the stringsends withsuffix.mystrings.Index(s, substr string) int: Returns the index of the first instance ofsubstrins, or -1 ifsubstris not present ins.mystrings.Join(elems []string, sep string) string: Concatenates the elements ofelemsto create a single string. The separator stringsepis placed between elements in the resulting string.mystrings.Replace(s, old, new string, n int) string: Returns a copy of the stringswith the firstnnon-overlapping instances ofoldreplaced bynew. Ifn < 0, there is no limit on the number of replacements.mystrings.Split(s, sep string) []string: Splits the stringsinto all substrings separated bysepand returns a slice of the substrings between those separators. Ifsepis empty,Splitsplits after each UTF-8 sequence. If bothsandsepare empty,Splitreturns an empty slice.
Key Requirements:
- Your implementations should be efficient and handle various input scenarios correctly.
- Pay attention to edge cases like empty strings, empty separators, and non-existent substrings.
- You are not allowed to use any functions from the standard
stringspackage itself for your implementations (e.g.,mystrings.Containscannot callstrings.Contains). You may use built-in Go language features and types.
Expected Behavior:
The output of each mystrings function should be identical to the output of its corresponding strings package function for the same inputs.
Important Edge Cases to Consider:
- Empty input strings (
s). - Empty substring/separator strings (
substr,sep). substrorsepbeing longer thans.nbeing zero or negative inReplace.sepappearing at the beginning or end ofsinSplit.- Consecutive
seps inSplit.
Examples
Example 1: mystrings.Contains
Input: s = "hello world", substr = "world"
Output: true
Explanation: The substring "world" is present in "hello world".
Input: s = "hello world", substr = "goodbye"
Output: false
Explanation: The substring "goodbye" is not present in "hello world".
Example 2: mystrings.Count
Input: s = "abababab", substr = "aba"
Output: 2
Explanation: The non-overlapping occurrences of "aba" are at index 0 and index 4.
Input: s = "aaaaa", substr = "aa"
Output: 2
Explanation: The non-overlapping occurrences of "aa" are at index 0 and index 2.
Example 3: mystrings.Index
Input: s = "hello world", substr = "world"
Output: 6
Explanation: "world" starts at index 6.
Input: s = "hello world", substr = "o"
Output: 4
Explanation: The first occurrence of "o" is at index 4.
Input: s = "hello world", substr = "z"
Output: -1
Explanation: "z" is not found in the string.
Example 4: mystrings.Join
Input: elems = ["apple", "banana", "cherry"], sep = ", "
Output: "apple, banana, cherry"
Explanation: The elements are joined with ", " as the separator.
Input: elems = ["a", "b", "c"], sep = ""
Output: "abc"
Explanation: Joining with an empty separator concatenates all elements.
Example 5: mystrings.Replace
Input: s = "foobarfoobar", old = "foo", new = "baz", n = 1
Output: "bazbarfoobar"
Explanation: Only the first occurrence of "foo" is replaced.
Input: s = "foobarfoobar", old = "foo", new = "baz", n = -1
Output: "bazbarbazbar"
Explanation: All occurrences of "foo" are replaced.
Input: s = "aaaaa", old = "aa", new = "b", n = 2
Output: "bba"
Explanation: The first two non-overlapping "aa" are replaced.
Example 6: mystrings.Split
Input: s = "a,b,c", sep = ","
Output: ["a", "b", "c"]
Explanation: The string is split by the comma.
Input: s = "apple//banana//cherry", sep = "//"
Output: ["apple", "banana", "cherry"]
Explanation: The string is split by "//".
Input: s = "///", sep = "/"
Output: ["", "", "", ""]
Explanation: Empty strings result from separators at the beginning, end, and between consecutive separators.
Input: s = "", sep = ","
Output: [""]
Explanation: Splitting an empty string by a non-empty separator results in a slice with one empty string.
Input: s = "abc", sep = ""
Output: ["a", "b", "c"]
Explanation: Splitting by an empty separator splits after each UTF-8 sequence.
Constraints
- Input strings (
s,substr,prefix,suffix,sep,old,new) will consist of ASCII characters or valid UTF-8 sequences. - The length of input strings will not exceed 1MB.
- The number of elements in
elemsforJoinwill not exceed 1000. - Your implementations should strive for reasonable performance. Avoid brute-force approaches that might lead to quadratic time complexity where linear or n*m (where n is length of s and m is length of substr) is achievable.
Notes
- Consider how Unicode characters might affect your implementations, especially when dealing with character positions and lengths.
- For functions like
IndexandContains, you'll need to iterate through the main string and check for the presence of the substring. Replacerequires careful handling of thenparameter and ensuring non-overlapping replacements.Splitis a bit more complex, especially the case where the separator is empty.- You are encouraged to create a
mystringspackage directory and place your implementations within it. - Think about how to efficiently compare parts of strings.