Hone logo
Hone
Problems

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 whether substr is within s.
  • mystrings.Count(s, substr string) int: Counts the number of non-overlapping instances of substr in s.
  • mystrings.HasPrefix(s, prefix string) bool: Reports whether the string s begins with prefix.
  • mystrings.HasSuffix(s, suffix string) bool: Reports whether the string s ends with suffix.
  • mystrings.Index(s, substr string) int: Returns the index of the first instance of substr in s, or -1 if substr is not present in s.
  • mystrings.Join(elems []string, sep string) string: Concatenates the elements of elems to create a single string. The separator string sep is placed between elements in the resulting string.
  • mystrings.Replace(s, old, new string, n int) string: Returns a copy of the string s with the first n non-overlapping instances of old replaced by new. If n < 0, there is no limit on the number of replacements.
  • mystrings.Split(s, sep string) []string: Splits the string s into all substrings separated by sep and returns a slice of the substrings between those separators. If sep is empty, Split splits after each UTF-8 sequence. If both s and sep are empty, Split returns 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 strings package itself for your implementations (e.g., mystrings.Contains cannot call strings.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).
  • substr or sep being longer than s.
  • n being zero or negative in Replace.
  • sep appearing at the beginning or end of s in Split.
  • Consecutive seps in Split.

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 elems for Join will 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 Index and Contains, you'll need to iterate through the main string and check for the presence of the substring.
  • Replace requires careful handling of the n parameter and ensuring non-overlapping replacements.
  • Split is a bit more complex, especially the case where the separator is empty.
  • You are encouraged to create a mystrings package directory and place your implementations within it.
  • Think about how to efficiently compare parts of strings.
Loading editor...
go