Program to insert dashes between two adjacent odd digits in given Number
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++
#include <iostream>
#include <string>
using namespace std;
bool checkOdd( char ch)
{
return ((ch - '0' ) & 1);
}
string Insert_dash(string num_str)
{
string result_str = num_str;
for ( int x = 0;
x < num_str.length() - 1; x++) {
if (checkOdd(num_str[x])
&& checkOdd(num_str[x + 1])) {
result_str.insert(x + 1, "-" );
num_str = result_str;
x++;
}
}
return result_str;
}
int main()
{
string str = "1745389" ;
cout << Insert_dash(str);
return 0;
}
|
Java
class GFG{
static boolean checkOdd( char ch)
{
return ((ch - '0' ) & 1 ) != 0 ?
true : false ;
}
static String Insert_dash(String num_str)
{
StringBuilder result_str = new StringBuilder(num_str);
for ( int x = 0 ; x < num_str.length() - 1 ; x++)
{
if (checkOdd(num_str.charAt(x)) &&
checkOdd(num_str.charAt(x + 1 )))
{
result_str.insert(x + 1 , "-" );
num_str = result_str.toString();
x++;
}
}
return result_str.toString();
}
public static void main(String[] args)
{
String str = "1745389" ;
System.out.println(Insert_dash(str));
}
}
|
Python3
def checkOdd(ch):
return (( ord (ch) - 48 ) & 1 )
def Insert_dash(num_str):
result_str = num_str
x = 0
while (x < len (num_str) - 1 ):
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
return result_str
str = "1745389"
print (Insert_dash( str ))
|
C#
using System;
using System.Text;
class GFG{
static bool checkOdd( char ch)
{
return ((ch - '0' ) & 1) != 0 ?
true : false ;
}
static String Insert_dash(String num_str)
{
StringBuilder result_str = new StringBuilder(num_str);
for ( int x = 0; x < num_str.Length - 1; x++)
{
if (checkOdd(num_str[x]) &&
checkOdd(num_str[x + 1]))
{
result_str.Insert(x + 1, "-" );
num_str = result_str.ToString();
x++;
}
}
return result_str.ToString();
}
public static void Main(String[] args)
{
String str = "1745389" ;
Console.WriteLine(Insert_dash(str));
}
}
|
Javascript
function checkOdd(ch)
{
return (parseInt(ch) & 1);
}
function Insert_dash(num_str)
{
let result_str = "" ;
for (let x = 0;
x < num_str.length - 1; x++) {
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];
return result_str;
}
let str = "1745389" ;
document.write(Insert_dash(str));
|
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++
#include <iostream>
#include <regex>
using namespace std;
string Insert_dash(string str)
{
const regex pattern( "([13579])([13579])" );
return regex_replace(str, pattern, "$1-$2" );;
}
int main()
{
string str = "1745389" ;
cout << Insert_dash(str);
return 0;
}
|
Java
import java.util.regex.*;
public class GFG {
public static String Insert_dash(String str)
{
String regex = "(?<=[13579])(?=[13579])" ;
Pattern pattern = Pattern.compile(regex);
Matcher matcher
= pattern.matcher(str);
String stringToBeReplaced = "-" ;
StringBuilder builder
= new StringBuilder();
return (matcher
.replaceAll(stringToBeReplaced));
}
public static void main(String[] args)
{
String str = "1745389" ;
System.out.println(Insert_dash(str));
}
}
|
Python
import re
def Insert_dash( str ):
regex = "(?<=[13579])(?=[13579])"
return re.sub(regex, '\1-\2' , str )
str = "1745389"
print (Insert_dash( str ))
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG {
static string Insert_dash( string str)
{
string pattern= "([13579])([13579])" ;
return Regex.Replace(str, pattern, "$1-$2" );;
}
public static void Main()
{
string str = "1745389" ;
Console.WriteLine(Insert_dash(str));
}
}
|
Javascript
function Insert_dash(str)
{
let regex = new RegExp( "([13579])([13579])" , 'g' );
return str.replace(regex, '$1-$2' );
}
let str = "1745389" ;
console.log(Insert_dash(str));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
27 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...