Reverse String II in C++ with O(n^2) Time Complexity


Given a string, write a function that reverses that string without using built-in functions or libraries.

Example:

Input:  "hello"

Output: "olleh"

Note:

Your algorithm should run in O(n^2) time and use O(n) extra space.


Problem Definition

In this problem, we are tasked with reversing a given string without using any built-in functions or libraries. The input is a single string, and the output should be the reversed version of that string.

Input:

A single string s.

Output:

A single string which is the reverse of s.

Constraints:

Example:

Input: "hello"
Output: "olleh"

Understanding the Problem

The core challenge of this problem is to reverse a string without using any built-in functions or libraries. This means we need to manually manipulate the string to achieve the desired result. Reversing a string is a common problem with applications in various fields such as data processing, text manipulation, and algorithms.

Potential pitfalls include handling edge cases such as empty strings or strings with a single character.

Approach

To solve this problem, we can use a simple approach where we create a new string and append characters from the original string in reverse order. This approach ensures that we do not use any built-in functions and adhere to the constraints.

Naive Solution

A naive solution would involve iterating through the string from the end to the beginning and appending each character to a new string. This approach is straightforward but may not be optimal in terms of time complexity.

Optimized Solution

Given the constraints, we need to ensure our solution runs in O(n^2) time complexity. One way to achieve this is by using a nested loop to construct the reversed string. Although this is not the most efficient way to reverse a string, it meets the problem's requirements.

Algorithm

Here is a step-by-step breakdown of the algorithm:

  1. Initialize an empty string reversed.
  2. Iterate through the original string from the last character to the first character.
  3. For each character, append it to the reversed string.
  4. Return the reversed string.

Code Implementation


#include <iostream>
#include <string>

std::string reverseString(const std::string &s) {
    // Initialize an empty string to store the reversed string
    std::string reversed;
    
    // Iterate through the original string from the end to the beginning
    for (int i = s.length() - 1; i >= 0; --i) {
        // Append each character to the reversed string
        reversed += s[i];
    }
    
    // Return the reversed string
    return reversed;
}

int main() {
    // Example usage
    std::string input = "hello";
    std::string output = reverseString(input);
    
    // Print the reversed string
    std::cout << "Reversed string: " << output << std::endl;
    
    return 0;
}

Complexity Analysis

The time complexity of this approach is O(n^2) because appending to a string in C++ can take O(n) time in the worst case, and we do this for each character in the string. The space complexity is O(n) because we are using an additional string to store the reversed string.

Edge Cases

Potential edge cases include:

Examples:

Input: ""
Output: ""

Input: "a"
Output: "a"

Testing

To test the solution comprehensively, consider the following test cases:

Using a variety of test cases ensures that the solution handles different scenarios effectively.

Thinking and Problem-Solving Tips

When approaching such problems, it is essential to:

Practicing similar problems and studying algorithms can help improve problem-solving skills.

Conclusion

Reversing a string without using built-in functions is a fundamental problem that helps in understanding string manipulation and algorithm design. By following the approach discussed, we can solve the problem within the given constraints. Practicing such problems enhances problem-solving skills and prepares for more complex challenges.

Additional Resources

For further reading and practice, consider the following resources: