blog.itcode.devblog.itcode.dev

[Programmers / JAVA] Level 1 Numeric String and English Words (81301)

Neo and Frodo are playing a number game. When Neo hands Frodo a card with a number where some digits have been replaced with English words, Frodo has to figure out the original number. Below is an example of replacing some digits of a number with English words.

[Programmers / JAVA] Level 1 Numeric String and English Words (81301)

Neo and Frodo are playing a number game. When Neo hands Frodo a card with a number where some digits have been replaced with English words, Frodo has to figure out the original number. Below is an example of replacing some digits of a number with English words.
RWB0104
@RWBwritten at 2021-12-14 02:21:04
Programmers

시리즈 모아보기

Programmers

3 / 78
RankLanguage Used
Level 1

🖼️ JAVA

🔗 Numeric String and English Words

Neo and Frodo are playing a number game. When Neo hands Frodo a card with a number where some digits have been replaced with English words, Frodo has to figure out the original number.

Below is an example of replacing some digits of a number with English words.

  • 1478 → one4seveneight
  • 234567 → 23four5six7
  • 10203 → 1zerotwozero3

You're given a string s as a parameter, where some digits of a number have been replaced with English words in this way, or possibly left unchanged. Complete the solution function so that it returns the original number represented by s.

For reference, the English word corresponding to each digit is shown in the table below.

DigitEnglish word
0zero
1one
2two
3three
4four
5five
6six
7seven
8eight
9nine
  • 1 ≤ length of s ≤ 50
  • s is never given starting with zero or 0.
  • s is only given as valid input where the return value is an integer between 1 and 2,000,000,000 inclusive.
sresult
one4seveneight1478
23four5six7234567
2three45sixseven234567
123123

Input/Output Example #1

Same as the problem example.

Input/Output Example #2

Same as the problem example.

Input/Output Example #3

Since "three" maps to 3, "six" maps to 6, and "seven" maps to 7, the answer is 234567, the same as Input/Output Example #2.

As shown in Input/Output Examples #2 and #3, there can be multiple different strings that point to the same answer.

Input/Output Example #4

There are no parts of s that have been replaced with English words.

  • Correctness test: 10 seconds

We need to implement an algorithm that converts all the lowercase English letters representing numbers back to digits, and ultimately returns the result as an int.

  1. Replace all the text in the string with digits.
  2. Cast the digitized string to int.

That's the two steps involved. There are several ways to do this, but since we're using JAVA anyway, I'd like to implement it using an enum object.

Applying an unfamiliar pattern to relatively simple logic is also good practice.

JAVA

private enum Number
{
	ZERO("0"),
	ONE("1"),
	TWO("2"),
	THREE("3"),
	FOUR("4"),
	FIVE("5"),
	SIX("6"),
	SEVEN("7"),
	EIGHT("8"),
	NINE("9");
	
	private final String num;
	
	Number(String num)
	{
		this.num = num;
	}
	
	public String getNum()
	{
		return num;
	}
}

Design the enum as shown above. Using Number.ONE.name().toLowerCase(), you can obtain the string one. Also, using Number.ONE.getNum(), you can obtain the digit 1 that corresponds to one. Use these two methods to replace the string.

JAVA

for (Number number : Number.values())
{
	answer = answer.replaceAll(number.name().toLowerCase(), number.getNum());
}

Number.values() returns all elements of Number in the form of an array. Using a for loop, replace each English word for a digit with its corresponding digit. Use replaceAll to replace all matching occurrences.

For the int conversion, let's use the Integer.parseInt() method.

JAVA

/**
 * Numeric String and English Words class
 *
 * @author RWB
 * @since 2021.12.06 Thu 18:47:19
 */
class Solution
{
	/**
	 * Method that returns the answer
	 *
	 * @param s: [String] The string
	 *
	 * @return [int] The answer
	 */
	public int solution(String s)
	{
		String answer = s;
		
		for (Number number : Number.values())
		{
			answer = answer.replaceAll(number.name().toLowerCase(), number.getNum());
		}
		
		return Integer.parseInt(answer);
	}
	
	/**
	 * Number enum
	 */
	private enum Number
	{
		ZERO("0"),
		ONE("1"),
		TWO("2"),
		THREE("3"),
		FOUR("4"),
		FIVE("5"),
		SIX("6"),
		SEVEN("7"),
		EIGHT("8"),
		NINE("9");
		
		private final String num;
		
		/**
		 * Constructor method
		 *
		 * @param num: [String] The digit
		 */
		Number(String num)
		{
			this.num = num;
		}
		
		/**
		 * Method that returns the digit
		 *
		 * @return [String] The digit
		 */
		public String getNum()
		{
			return num;
		}
	}
}

If you'd rather not use enum, you can also implement this with more familiar structures like a Map or an array.

For example, code like int nums[] = new int[] { "zero", "one" ... "nine" }; would let you build the same logic using an array's index and its value.

Since nums[0] = "zero", it's also possible to perform the replacement by appropriately using the index and the value.

# Programmers# Algorithm# JAVA# Level 1
ship
blog.itcode.dev

Notes from the π-th Alpaca

7.0.1
Developed by RWB since 2021.057th upgraded at 2026.08