. Integer to English Words

PHPz
Release: 2024-08-08 07:00:12
Original
510 people have browsed it

. Integer to English Words

273. Integer to English Words

Hard

Topics: Math, String, Recursion

Convert a non-negative integer num to its English words representation.

Example 1:

  • Input:num = 123
  • Output:"One Hundred Twenty Three"

Example 2:

  • Input:num = 12345
  • Output:"Twelve Thousand Three Hundred Forty Five"

Example 3:

  • Input:num = 1234567
  • Output:"One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

Constraints:

  • 0 <= num <= 231- 1

Hint:

  1. Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
  2. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
  3. There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)

Solution:

To solve this problem, we can follow these steps:

  1. Define the words for numbers:We need arrays for the words representing single digits, teens, tens, and the thousands groupings.

  2. Create a helper function:This function will handle numbers less than 1000, converting them to English words.

  3. Recursive function:The main function will recursively process chunks of the number, adding the appropriate thousand group label (e.g., Thousand, Million, Billion).

  4. Edge cases:Handle edge cases like 0 and numbers where intermediate chunks are zero.

Let's implement this solution in PHP:273. Integer to English Words

        

Explanation:

  1. Main Function (numberToWords):

    • Checks if the input number is 0 and returns "Zero".
    • Initializes the thousands array with the labels for a thousand groupings.
    • Iteratively processes the number in chunks of thousands, using the helper function to convert each chunk to words.
    • Constructs the final result string by combining the words for each chunk with the appropriate thousand group label.
  2. Helper Function (helper):

    • Uses predefined arrays for numbers below 20 and for the tens multiples.
    • Recursively constructs the English words for numbers less than 1000:
      • For numbers less than 20, directly returns the corresponding word.
      • For numbers less than 100, combines the word for the tens place with the result of a recursive call for the units place.
      • For numbers 100 and above, combines the word for the hundreds place with the result of a recursive call for the remainder.

This solution handles the constraints and edge cases effectively, providing the correct English words representation for any number within the given range.

Contact Links

If you found this series helpful, please consider giving therepositorya star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

  • LinkedIn
  • GitHub

The above is the detailed content of . Integer to English Words. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!