Convert given string to a valid mobile number
Last Updated :
20 Aug, 2021
Given a string M consisting of letters, digits and symbols, the task is to convert the string to a valid mobile number by removing all characters except the digits in the following format:
- Form a substring of 3 digits while the length of the remaining string is greater than 3.
- Enclose each substring with brackets “()” and separate them with “-“.
If a valid mobile number cannot be obtained, i.e. if the string does not consist of 10 digits, then print -1. Otherwise, print the string obtained.
Examples:
Input: M = “91 234rt5%34*0 3”
Output: “(912)-(345)-(340)-(3)”
Explanation: After removing all extra characters, M = “9123453403”. Therefore, the final string obtained is “(912)-(345)-(340)-(3)”.
Input: M=”9 9 ry7%64 9 7″
Output: “Invalid”
Explanation: After removing extra characters M=”9976497″. Since the length of the string is not equal to 10, the required output is -1.
Approach: Follow the below steps to solve the problem:
- Initialize a string, say S and append all digits of M in S in the given order.
- Now, if the length of S is not 10, print “Invalid”, and end the program.
- Otherwise, if the length of the string S is 10, make a group of 3 characters and enclose it within the brackets “()” and make separate it with “-“.
- Print final string as S.
Below is the solution for the above problem.
C++
#include <bits/stdc++.h>
using namespace std;
void Validate(string M)
{
int len = M.size();
string temp = "" ;
for ( int i = 0; i < len; i++) {
if ( isdigit (M[i]))
temp += M[i];
}
int nwlen = temp.size();
if (nwlen != 10) {
cout << "Invalid\n" ;
return ;
}
string res = "" ;
string x = temp.substr(0, 3);
res += "(" + x + ")-" ;
x = temp.substr(3, 3);
res += "(" + x + ")-" ;
x = temp.substr(6, 3);
res += "(" + x + ")-" ;
x = temp.substr(9, 1);
res += "(" + x + ")" ;
cout << res << "\n" ;
}
int main()
{
string M = "91 234rt5%34*0 3" ;
Validate(M);
}
|
Java
import java.util.*;
class GFG
{
static void Validate(String M)
{
int len = M.length();
String temp = "" ;
for ( int i = 0 ; i < len; i++)
{
if (Character.isDigit(M.charAt(i)))
temp += M.charAt(i);
}
int nwlen = temp.length();
if (nwlen != 10 )
{
System.out.print( "Invalid\n" );
return ;
}
String res = "" ;
String x = temp.substring( 0 , 3 );
res += "(" + x + ")-" ;
x = temp.substring( 3 , 6 );
res += "(" + x + ")-" ;
x = temp.substring( 6 , 9 );
res += "(" + x + ")-" ;
x = temp.substring( 9 , 10 );
res += "(" + x + ")" ;
System.out.print(res+ "\n" );
}
public static void main(String[] args)
{
String M = "91 234rt5%34*0 3" ;
Validate(M);
}
}
|
Python3
def Validate(M):
lenn = len (M)
temp = ""
for i in range (lenn):
if (M[i].isdigit()):
temp + = M[i]
nwlenn = len (temp)
if (nwlenn ! = 10 ):
print ( "Invalid" )
return
res = ""
x = temp[ 0 : 3 ]
res + = "(" + x + ")-"
x = temp[ 3 : 3 + 3 ]
res + = "(" + x + ")-"
x = temp[ 6 : 3 + 6 ]
res + = "(" + x + ")-"
x = temp[ 9 : 1 + 9 ]
res + = "(" + x + ")"
print (res)
if __name__ = = '__main__' :
M = "91 234rt5%34*0 3"
Validate(M)
|
C#
using System;
class GFG
{
static void Validate( string M)
{
int len = M.Length;
string temp = "" ;
for ( int i = 0; i < len; i++)
{
if (Char.IsDigit(M[i]))
temp += M[i];
}
int nwlen = temp.Length;
if (nwlen != 10)
{
Console.Write( "Invalid\n" );
return ;
}
string res = "" ;
string x = temp.Substring(0, 3);
res += "(" + x + ")-" ;
x = temp.Substring(3, 3);
res += "(" + x + ")-" ;
x = temp.Substring(6, 3);
res += "(" + x + ")-" ;
x = temp.Substring(9, 1);
res += "(" + x + ")" ;
Console.WriteLine(res);
}
public static void Main( string [] args)
{
string M = "91 234rt5%34*0 3" ;
Validate(M);
}
}
|
Javascript
<script>
function Validate(M)
{
let len = M.length;
let temp = "" ;
for (let i = 0; i < len; i++)
{
if (! isNaN( parseInt(M[i]) ))
temp += M[i];
}
let nwlen = temp.length;
if (nwlen != 10)
{
document.write( "Invalid<br>" );
return ;
}
let res = "" ;
let x = temp.substring(0, 3);
res += "(" + x + ")-" ;
x = temp.substring(3, 6);
res += "(" + x + ")-" ;
x = temp.substring(6, 9);
res += "(" + x + ")-" ;
x = temp.substring(9, 10);
res += "(" + x + ")" ;
document.write(res+ "<br>" );
}
let M = "91 234rt5%34*0 3" ;
Validate(M);
</script>
|
Output:
(912)-(345)-(340)-(3)
Time Complexity: O(|M|+|S|)
Auxiliary Space: O(|M|+|S|)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...