Open In App

Convert textual Phone Number to 10 digit numerical number

Improve
Improve
Like Article
Like
Save
Share
Report

Given a string S of size N containing lowercase English letters, representing a phone number(all phone numbers will be 10 digits) in words, the task is to convert the number into digits. Repeating digits can be shortened as follows: 

  • If any digit repeats two times then in words is written as  “double”.
  • If any digit repeats three times then in words is written as  “triple”.
  • If any digit repeats four times then in words is written as  “quad”.
  • If any digit repeats five times then in words is written as  “penta”.
  • If any digit repeats six times then in words is written as  “hexa”.
  • If any digit repeats seven times then in words is written as  “hepta”.
  • If any digit repeats eight times then in words is written as  “octa”.
  • If any digit repeats nine times then in words is written as  “nona”.
  • If any digit repeats ten times then in words is written as  “deca”.

Examples:

Input: “five eight quad two four triple eight”
Output: “5822224888”
Explanation: Here is one 5, one 8, four 2, one 4, and three 8.

Input: “five one zero two four eight zero double three two”
Output: “5102480332”

Input: “five one zero six triple eight nine six four”
Output: “5106888964”

Input: “five eight double two double two four eight five six”
Output: “5822224856”

 

Approach: This is an implementation based problem that can be solved based on the following idea:

Consider each word as a substring. if that is of the type which represents a repetition, then repeat the next digit that many times. Otherwise, convert that word to the corresponding digit. Follow the same for all the words and form the number. 

 Follow the below steps to implement the idea:

  • Create a temporary string (say resnum) to store the number.
  • Pick up each word:
    • If the word is of the repetition types i.e., “double“, “triple“, “quad“, “penta“, “hexa“, “hepta“, “octa“, “nona” and “deca“, convert them into its integer form and represent that as the frequency of the next digit.
    • Otherwise, convert the word into its corresponding digit and repeat (if there is any repetition term mentioned before this) and add that to resnum.
  • .Return resnum as the final answer.

Below is the implementation for the above approach:

C++14




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to convert double, triple, quad etc
// into 2, 3, 4 etc respectively
int prefixFreq(string& word)
{
    if (word.compare("double") == 0)
        return 2;
    else if (word.compare("triple") == 0)
        return 3;
    else if (word.compare("quad") == 0)
        return 4;
    else if (word.compare("penta") == 0)
        return 5;
    else if (word.compare("hexa") == 0)
        return 6;
    else if (word.compare("hepta") == 0)
        return 7;
    else if (word.compare("octa") == 0)
        return 8;
    else if (word.compare("nona") == 0)
        return 9;
    else if (word.compare("deca") == 0)
        return 10;
 
    return -1;
}
 
// Function to convert zero, one, two etc
// into 0, 1, 2 respectively
char strTOnum(string& word)
{
    if (word.compare("zero") == 0)
        return '0';
    else if (word.compare("one") == 0)
        return '1';
    else if (word.compare("two") == 0)
        return '2';
    else if (word.compare("three") == 0)
        return '3';
    else if (word.compare("four") == 0)
        return '4';
    else if (word.compare("five") == 0)
        return '5';
    else if (word.compare("six") == 0)
        return '6';
    else if (word.compare("seven") == 0)
        return '7';
    else if (word.compare("eight") == 0)
        return '8';
    else if (word.compare("nine") == 0)
        return '9';
 
    return -1;
}
 
// Function to extract the number from string
string TextToNum(string& TXTNO)
{
    string word;
    string resnum;
    vector<string> strarr;
    int i = 0, j = 0;
    int temp;
 
    stringstream ss(TXTNO);
    while (ss >> word)
        strarr.push_back(word);
 
    for (int i = 0; i < strarr.size(); i++) {
        temp = prefixFreq(strarr[i]);
 
        if (temp == -1)
            resnum += strTOnum(strarr[i]);
 
        else {
            for (j = 0; j < temp; j++)
                resnum += strTOnum(strarr[i + 1]);
 
            i++;
        }
    }
 
    return resnum;
}
 
// Driver code
int main()
{
    string s = "five eight quad two four "
               "triple eight";
       
    // Function call
    cout << TextToNum(s);
    return 0;
}


Java




/*package whatever //do not write package name here */
// Java code to implement the approach
import java.io.*;
import java.util.Arrays;
import java.util.List;
 
class GFG {
    // Function to convert double, triple, quad etc
    // into 2, 3, 4 etc respectively
    public static int prefixFreq(String word)
    {
        if (word.equals("double"))
            return 2;
        else if (word.equals("triple"))
            return 3;
        else if (word.equals("quad"))
            return 4;
        else if (word.equals("penta"))
            return 5;
        else if (word.equals("hexa"))
            return 6;
        else if (word.equals("hepta"))
            return 7;
        else if (word.equals("octa"))
            return 8;
        else if (word.equals("nona"))
            return 9;
        else if (word.equals("deca"))
            return 10;
 
        return -1;
    }
 
    // Function to convert zero, one, two etc
    // into 0, 1, 2 respectively
    public static char strTOnum(String word)
    {
        if (word.equals("zero"))
            return '0';
        else if (word.equals("one"))
            return '1';
        else if (word.equals("two"))
            return '2';
        else if (word.equals("three"))
            return '3';
        else if (word.equals("four"))
            return '4';
        else if (word.equals("five"))
            return '5';
        else if (word.equals("six"))
            return '6';
        else if (word.equals("seven"))
            return '7';
        else if (word.equals("eight"))
            return '8';
        else if (word.equals("nine"))
            return '9';
 
        return (char)-1;
    }
 
    // Function to extract the number from string
    public static String TextToNum(String TXTNO)
    {
        String word;
        String resnum = "";
        List<String> strarr
            = Arrays.asList(TXTNO.split(" "));
        int i = 0, j = 0;
        int temp;
 
        for (i = 0; i < strarr.size(); i++) {
            temp = prefixFreq(strarr.get(i));
 
            if (temp == -1)
                resnum += strTOnum(strarr.get(i));
 
            else {
                for (j = 0; j < temp; j++)
                    resnum += strTOnum(strarr.get(i + 1));
 
                i++;
            }
        }
 
        return resnum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String s = "five eight quad two four triple eight";
        System.out.println(TextToNum(s));
    }
}
 
// This code is contributed by kaalel.


Python3




# Python3 code to implement the approach
 
# Function to convert double, triple, quad etc
# into 2, 3, 4 etc respectively
 
 
def prefixFreq(word):
    if (word == "double"):
        return 2
    elif (word == "triple"):
        return 3
    elif (word == "quad"):
        return 4
    elif (word == "penta"):
        return 5
    elif (word == "hexa"):
        return 6
    elif (word == "hepta"):
        return 7
    elif (word == "octa"):
        return 8
    elif (word == "nona"):
        return 9
    elif (word == "deca"):
        return 10
    return -1
 
 
def strTOnum(word):
    if word == "zero":
        return '0'
    if word == "one":
        return '1'
    if word == "two":
        return '2'
    if word == "three":
        return '3'
    if word == "four":
        return '4'
    if word == "five":
        return '5'
    if word == "six":
        return '6'
    if word == "seven":
        return '7'
    if word == "eight":
        return '8'
    if word == "nine":
        return '9'
    return -1
 
# function to extract the number from string
 
 
def TextToNum(TXTNO):
    resnum = ""
    strarr = TXTNO.split()
    i = 0
    while i < len(strarr):
        temp = prefixFreq(strarr[i])
 
        if temp == -1:
            resnum += strTOnum(strarr[i])
        else:
            for j in range(temp):
                resnum += strTOnum(strarr[i + 1])
            i += 1
        i += 1
 
    return resnum
 
 
# Driver Code
s = "five eight quad two four triple eight"
 
# function call
print(TextToNum(s))
 
# this code is contributed by phasing17


C#




// C# program to implement above approach
using System;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to convert double, triple, quad etc
  // into 2, 3, 4 etc respectively
  static int prefixFreq(String word)
  {
    if (word.Equals("double"))
      return 2;
    else if (word.Equals("triple"))
      return 3;
    else if (word.Equals("quad"))
      return 4;
    else if (word.Equals("penta"))
      return 5;
    else if (word.Equals("hexa"))
      return 6;
    else if (word.Equals("hepta"))
      return 7;
    else if (word.Equals("octa"))
      return 8;
    else if (word.Equals("nona"))
      return 9;
    else if (word.Equals("deca"))
      return 10;
 
    return -1;
  }
 
  // Function to convert zero, one, two etc
  // into 0, 1, 2 respectively
  static char strTOnum(String word)
  {
    if (word.Equals("zero"))
      return '0';
    else if (word.Equals("one"))
      return '1';
    else if (word.Equals("two"))
      return '2';
    else if (word.Equals("three"))
      return '3';
    else if (word.Equals("four"))
      return '4';
    else if (word.Equals("five"))
      return '5';
    else if (word.Equals("six"))
      return '6';
    else if (word.Equals("seven"))
      return '7';
    else if (word.Equals("eight"))
      return '8';
    else if (word.Equals("nine"))
      return '9';
 
    return (char)0;
  }
 
  // Function to extract the number from string
  static String TextToNum(String TXTNO)
  {
    String resnum = "";
    List<String> strarr = TXTNO.Split(" ").ToList();
    int i = 0, j = 0;
    int temp;
 
    for (i = 0 ; i < strarr.Count ; i++) {
      temp = prefixFreq(strarr[i]);
 
      if (temp == -1)
        resnum += strTOnum(strarr[i]);
 
      else {
        for (j = 0 ; j < temp ; j++){
          resnum += strTOnum(strarr[i+1]);
        }
 
        i++;
      }
    }
 
    return resnum;
  }
 
  // Driver Code
  public static void Main(string[] args){
 
    String s = "five eight quad two four triple eight";
    Console.WriteLine(TextToNum(s));
 
  }
}
 
// This code is contributed by subhamgoyal2014.


Javascript




<Script>
// JavaScript code to implement the approach
 
// Function to convert double, triple, quad etc
// into 2, 3, 4 etc respectively
function prefixFreq(word)
{
    if (word == "double")
        return 2;
    else if (word == "triple")
        return 3;
    else if (word == "quad")
        return 4;
    else if (word == "penta")
        return 5;
    else if (word == "hexa")
        return 6;
    else if (word == "hepta")
        return 7;
    else if (word == "octa")
        return 8;
    else if (word == "nona")
        return 9;
    else if (word == "deca")
        return 10;
    return -1;
}
 
 
function strTOnum(word)
{
    if (word == "zero")
        return '0';
    if (word == "one")
        return '1';
    if (word == "two")
        return '2';
    if (word == "three")
        return '3';
    if (word == "four")
        return '4';
    if (word == "five")
        return '5';
    if (word == "six")
        return '6';
    if (word == "seven")
        return '7';
    if (word == "eight")
        return '8';
    if (word == "nine")
        return '9';
    return -1;
}
 
// function to extract the number from string
function TextToNum(TXTNO)
{
    var resnum = "";
    var strarr = TXTNO.split(" ");
    var i = 0;
    while (i < strarr.length)
    {
        var temp = prefixFreq(strarr[i]);
 
        if (temp == -1)
            resnum += strTOnum(strarr[i]);
        else
        {
            for (var j = 0; j < temp; j++)
                resnum += strTOnum(strarr[i + 1]);
            i += 1;
        }
        i += 1;
    }
 
    return resnum;
}
 
// Driver Code
var s = "five eight quad two four triple eight";
 
// function call
document.write(TextToNum(s));
 
// This code is contributed by phasing17
</script>


Output

5822224888

Time Complexity: O(N)
Auxiliary Space: O(N)



Last Updated : 06 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads