![]() |
VOOZH | about |
Given a large number in form of string N, the task is to insert a dash between two adjacent odd digits in the given number in form of strings.
Examples:
Input: N = 1745389
Output: 1-745-389
Explanation:
In string str, str[0] and str[1] both are the odd numbers in consecutive, so insert a dash between them.
Input: N = 34657323128437
Output: 3465-7-323-12843-7
Bitwise Approach:
Below is the implementation of the above approach:
1-745-389
Time Complexity: O(N)
Auxiliary Space: O(1)
Regular Expression Approach:
The given problem can be solved using Regular Expression. The RE for this problem will be:
(?<=[13579])(?=[13579])
The given RE matches between odd numbers. We can replace the matched part of zero width with a dash, i.e.
str = str.replaceAll("(?<=[13579])(?=[13579])", "-");
Below is the implementation of the above approach:
1-745-389
Time Complexity: O(N)
Auxiliary Space: O(1)