Regex Substitution - HackerRank Solution Python
Tags: python, algorithms, beginners, coding
Published: 2023-09-09 03:25:54

Regex Substitution - HackerRank is a problem from HackerRank in the domain Python subdomain Regex called Regex Substitution. It’s one of the medium level problems of Python. We have to solve this problem in python.

## Problem Statement and Explanation

In this problem, we have to use `regular expression` to substitute all `&&` and `||` symbols with `and` and `or` respectively using `re.sub()` method.

### Input Format

- The first line contains the integer, `N`. The next `N` lines each contain a line of the text.
- The next N lines contains the `HTML` code.

### Output Format

Print the modified `HTML` code for each test case as a single line and the modified `HTML` code should be printed as it was received as input.

## Regex Substitution Solution in Python

{% gist https://gist.github.com/codeperfectplus/883b1d8c5e0e9bc5c7d0b7d512794366.js %}

## Explanation of Solution

- The first line imports the re module, which provides regular expression functionality in Python.

- The `substitute_text_in_string()` function takes a string as input and returns a new string with the `&&` and `||` characters replaced with and and or, respectively.

- The function works by first creating a regular expression pattern that matches the `&&` and `||` characters, followed by two spaces.

- The re.sub() function is then used to substitute the pattern with the replacement text, which is either and or or.

- The lambda expression is used to determine the replacement text, depending on the value of the matched group.

- The if **name** == ‘ **main** ’: block is used to execute the function when the code is run as a script.

- The first -line in the block prompts the user to enter the number of lines of text.

- The next line creates an empty string to store the input text.

- The following loop iterates through the number of lines and appends each line to the text string.

- The last line calls the substitute\_text\_in\_string() function to replace the `&&` and `||` characters in the text string.

## Complexity of the Solution

### Time complexity

The time complexity of the solution is O(n), where n is the number of lines of text. This is because the substitute\_text\_in\_string() function is called n times, once for each line of text.

The re.sub() function takes O(m) time, where m is the length of the regular expression pattern.

In this case, the regular expression pattern is of length 8, so the re.sub() function takes O(8) time. Therefore, the overall time complexity of the solution is O(n + 8) = O(n).

### Space complexity

The space complexity of the solution is O(n), where n is the number of lines of text.

This is because the substitute\_text\_in\_string() function creates a new string with the `&&` and `||` characters replaced. The new string is of length n, so the space complexity of the solution is O(n).

In conclusion, the time and space complexity of the solution are both O(n).

_Problem statement is taken from [Hackerrank](https://www.hackerrank.com/domains/python), and the solutions are implemented by **CodePerfectPlus** team_