![]() |
Given a number, our task is to take an input integer and replace all occurrences of the digit ‘0’ with the digit ‘5’. We can solve this problem by using various methods in JavaScript. Example: Input: Below are the approaches to replace all ‘0’ with ‘5’ in an input integer using JavaScript: Table of Content String Conversion and ManipulationThis approach converts the input integer into a string using the toString() method. Then, it replaces all occurrences of ‘0’ with ‘5’ using the replace() method with a regular expression /0/g. Finally, it parses the modified string back to an integer using parseInt() with a radix of 10 for decimal conversion. Example: The example below shows the replacement of all ‘0’ with ‘5’ in an input integer using String Conversion and Manipulation.
Output 1525 Time Complexity: O(n), where n is the number of digits in the integer. Space Complexity: O(n). Mathematical ManipulationThe function replaceZerosWithMath iterates through each digit of the input integer ‘n’, replaces ‘0’ with ‘5’, and constructs the resulting integer using mathematical manipulation. It initializes ‘result’ to 0 and ‘multiplier’ to 1, then iterates through each digit of ‘n’, replacing ‘0’ with ‘5’ and building the result by multiplying each digit with ‘multiplier’ and adding it to ‘result’. Finally, it returns the resulting integer after processing all digits. Example: The example below illustrates the replacement of all ‘0’ with ‘5’ in an input integer using Mathematical Manipulation.
Output 1525 Time Complexity: O(n), where n is the number of digits in the integer. Space Complexity: O(1). Iterative with ReversalThe function ‘replaceZeroWithFive’ iterates through each digit of the input integer ‘num’. It replaces every ‘0’ digit with ‘5’ by building a temporary integer ‘temp’ with reversed digits containing ‘5’ instead of ‘0’. It then reverses ‘temp’ to obtain the final result integer ‘reversed’ and returns it. Example: The example below shows the replacement of all ‘0’ with ‘5’ in an input integer using the Iterative with Reversal method.
Output 9575325553 Time Complexity: O(n), where n is the number of digits in the integer. Space Complexity: O(1). RecursionThe function replaceZeroWithFive recursively replaces each ‘0’ digit in the input integer num with ‘5’. It checks if num is equal to 0, in which case it returns 5. Otherwise, it extracts the last digit of the num, replaces ‘0’ with ‘5’ if necessary, and recursively calls itself with the remaining digits. Finally, it concatenates the modified digit with the result of the recursive call to build the final integer with ‘0’ replaced by ‘5’. Example: The example below shows the replacement of all ‘0’ with ‘5’ in an input integer using the Iterative with the Recursion method.
Output 5354553592 Time Complexity: O(n), where n is the number of digits in the integer. Space Complexity: O(n). |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |