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:
- Traverse the whole string of numbers character by character.
- Compare every consecutive character using logical Bitwise OR and AND operators.
- If two consecutive characters of the string are odd, insert a dash (-) in them and check for the next two consecutive characters.
Below is the implementation of the above approach:
// C++ program for the above approach
#include <iostream>
#include <string>
using namespace std;
// Function to check if char ch is
// odd or not
bool checkOdd(char ch)
{
return ((ch - '0') & 1);
}
// Function to insert dash - between
// any 2 consecutive digit in string str
string Insert_dash(string num_str)
{
string result_str = num_str;
// Traverse the string character
// by character
for (int x = 0;
x < num_str.length() - 1; x++) {
// Compare every consecutive
// character with the odd value
if (checkOdd(num_str[x])
&& checkOdd(num_str[x + 1])) {
result_str.insert(x + 1, "-");
num_str = result_str;
x++;
}
}
// Print the resultant string
return result_str;
}
// Driver Code
int main()
{
// Given number in form of string
string str = "1745389";
// Function Call
cout << Insert_dash(str);
return 0;
}
// Java program to implement
// the above approach
class GFG{
// Function to check if char ch is
// odd or not
static boolean checkOdd(char ch)
{
return ((ch - '0') & 1) != 0 ?
true : false;
}
// Function to insert dash - between
// any 2 consecutive digit in string str
static String Insert_dash(String num_str)
{
StringBuilder result_str = new StringBuilder(num_str);
// Traverse the string character
// by character
for(int x = 0; x < num_str.length() - 1; x++)
{
// Compare every consecutive
// character with the odd value
if (checkOdd(num_str.charAt(x)) &&
checkOdd(num_str.charAt(x + 1)))
{
result_str.insert(x + 1, "-");
num_str = result_str.toString();
x++;
}
}
// Print the resultant string
return result_str.toString();
}
// Driver Code
public static void main(String[] args)
{
// Given number in form of string
String str = "1745389";
// Function call
System.out.println(Insert_dash(str));
}
}
// This code is contributed by rutvik_56
# Python3 program for the above approach
# Function to check if char ch is
# odd or not
def checkOdd(ch):
return ((ord(ch) - 48) & 1)
# Function to insert dash - between
# any 2 consecutive digit in string str
def Insert_dash(num_str):
result_str = num_str
# Traverse the string character
# by character
x = 0
while(x < len(num_str) - 1):
# Compare every consecutive
# character with the odd value
if (checkOdd(num_str[x]) and
checkOdd(num_str[x + 1])):
result_str = (result_str[:x + 1] + '-' +
result_str[x + 1:])
num_str = result_str
x += 1
x += 1
# Print the resultant string
return result_str
# Driver Code
# Given number in form of string
str = "1745389"
# Function call
print(Insert_dash(str))
# This code is contributed by vishu2908
// C# program to implement
// the above approach
using System;
using System.Text;
class GFG{
// Function to check if char ch is
// odd or not
static bool checkOdd(char ch)
{
return ((ch - '0') & 1) != 0 ?
true : false;
}
// Function to insert dash - between
// any 2 consecutive digit in string str
static String Insert_dash(String num_str)
{
StringBuilder result_str = new StringBuilder(num_str);
// Traverse the string character
// by character
for(int x = 0; x < num_str.Length - 1; x++)
{
// Compare every consecutive
// character with the odd value
if (checkOdd(num_str[x]) &&
checkOdd(num_str[x + 1]))
{
result_str.Insert(x + 1, "-");
num_str = result_str.ToString();
x++;
}
}
// Print the resultant string
return result_str.ToString();
}
// Driver Code
public static void Main(String[] args)
{
// Given number in form of string
String str = "1745389";
// Function call
Console.WriteLine(Insert_dash(str));
}
}
// This code is contributed by Rajput-Ji
// Javascript program for the above approach
// Function to check if char ch is
// odd or not
function checkOdd(ch)
{
return (parseInt(ch) & 1);
}
// Function to insert dash - between
// any 2 consecutive digit in string str
function Insert_dash(num_str)
{
let result_str = "";
// Traverse the string character
// by character
for (let x = 0;
x < num_str.length - 1; x++) {
// Compare every consecutive
// character with the odd value
if (checkOdd(num_str[x])
&& checkOdd(num_str[x + 1])) {
result_str+=(num_str[x]+ "-");
}
else{
result_str+=num_str[x];
}
}
result_str+=num_str[num_str.length-1];
// Print the resultant string
return result_str;
}
// Driver Code
// Given number in form of string
let str = "1745389";
// Function Call
document.write(Insert_dash(str));
Output:
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:
// C++ Program to implement
// the above approach
#include <iostream>
#include <regex>
using namespace std;
// Function to insert dash - between
// any 2 consecutive odd digit
string Insert_dash(string str)
{
// Get the regex to be checked
const regex pattern("([13579])([13579])");
// Replaces the matched value
// (here dash) with given string
return regex_replace(str, pattern, "$1-$2");;
}
// Driver Code
int main()
{
string str = "1745389";
cout << Insert_dash(str);
return 0;
}
// This code is contributed by yuvraj_chandra
// Java program for the above approach
import java.util.regex.*;
public class GFG {
// Function to insert dash - between
// any 2 consecutive odd digit
public static String Insert_dash(String str)
{
// Get the regex to be checked
String regex = "(?<=[13579])(?=[13579])";
// Create a pattern from regex
Pattern pattern = Pattern.compile(regex);
// Create a matcher for the input String
Matcher matcher
= pattern.matcher(str);
// Get the String to be replaced,
// i.e. here dash
String stringToBeReplaced = "-";
StringBuilder builder
= new StringBuilder();
// Replace every matched pattern
// with the target String
// using replaceAll() method
return (matcher
.replaceAll(stringToBeReplaced));
}
// Driver Code
public static void main(String[] args)
{
// Given number in form of string
String str = "1745389";
// Function Call
System.out.println(Insert_dash(str));
}
}
# Python program for the above approach
import re
# Function to insert dash - between
# any 2 consecutive odd digit
def Insert_dash(str):
# Get the regex to be checked
regex = "(?<=[13579])(?=[13579])"
return re.sub(regex,'\1-\2', str)
# Driver Code
# Given number in form of string
str = "1745389"
# Function Call
print(Insert_dash(str))
# This code is contributed by yuvraj_chandra
// C# Program to implement
// the above approach
using System;
using System.Text.RegularExpressions;
public class GFG {
// Function to insert dash - between
// any 2 consecutive odd digit
static string Insert_dash(string str)
{
// Get the regex to be checked
string pattern="([13579])([13579])";
// Replaces the matched value
// (here dash) with given string
return Regex.Replace(str, pattern, "$1-$2");;
}
// Driver Code
public static void Main()
{
string str = "1745389";
Console.WriteLine(Insert_dash(str));
}
}
// This code is contributed by Aman Kumar.
// Javascript Program to implement
// the above approach
// Function to insert dash - between
// any 2 consecutive odd digit
function Insert_dash(str)
{
// Get the regex to be checked
let regex = new RegExp("([13579])([13579])",'g');
// Replaces the matched value
// (here dash) with given string
return str.replace(regex, '$1-$2');
}
// Driver Code
let str = "1745389";
console.log(Insert_dash(str));
// This code is contributed by Pushpesh Raj.
Output:
1-745-389
Time Complexity: O(N)
Auxiliary Space: O(1)