LeetCode Problem | LeetCode Problems For Beginners | LeetCode Problems & Solutions | Improve Problem Solving Skills | LeetCode Problems Java | LeetCode Solutions in C++
Hello Programmers/Coders, Today we are going to share solutions to the Programming problems of LeetCode Solutions in C++, Java, & Python. At Each Problem with Successful submission with all Test Cases Passed, you will get a score or marks and LeetCode Coins. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for the Valid Number in C++, Java & Python-LeetCode problem. We are providing the correct and tested solutions to coding problems present on LeetCode. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
About LeetCode
LeetCode is one of the most well-known online judge platforms to help you enhance your skills, expand your knowledge and prepare for technical interviews.
LeetCode is for software engineers who are looking to practice technical questions and advance their skills. Mastering the questions in each level on LeetCode is a good way to prepare for technical interviews and keep your skills sharp. They also have a repository of solutions with the reasoning behind each step.
LeetCode has over 1,900 questions for you to practice, covering many different programming concepts. Every coding problem has a classification of either Easy, Medium, or Hard.
LeetCode problems focus on algorithms and data structures. Here is some topic you can find problems on LeetCode:
- Mathematics/Basic Logical Based Questions
- Arrays
- Strings
- Hash Table
- Dynamic Programming
- Stack & Queue
- Trees & Graphs
- Greedy Algorithms
- Breadth-First Search
- Depth-First Search
- Sorting & Searching
- BST (Binary Search Tree)
- Database
- Linked List
- Recursion, etc.
Leetcode has a huge number of test cases and questions from interviews too like Google, Amazon, Microsoft, Facebook, Adobe, Oracle, Linkedin, Goldman Sachs, etc. LeetCode helps you in getting a job in Top MNCs. To crack FAANG Companies, LeetCode problems can help you in building your logic.
Link for the Problem – Valid Number– LeetCode Problem
Valid Number– LeetCode Problem
Problem:
A valid number can be split up into these components (in order):
- A decimal number or an integer.
- (Optional) An
'e'
or'E'
, followed by an integer.
A decimal number can be split up into these components (in order):
- (Optional) A sign character (either
'+'
or'-'
). - One of the following formats:
- One or more digits, followed by a dot
'.'
. - One or more digits, followed by a dot
'.'
, followed by one or more digits. - A dot
'.'
, followed by one or more digits.
- One or more digits, followed by a dot
An integer can be split up into these components (in order):
- (Optional) A sign character (either
'+'
or'-'
). - One or more digits.
For example, all the following are valid numbers: ["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]
, while the following are not valid numbers: ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]
.
Given a string s
, return true
if s
is a valid number.
Example 1:
Input: s = "0" Output: true
Example 2:
Input: s = "e" Output: false
Example 3:
Input: s = "." Output: false
Constraints:
1 <= s.length <= 20
s
consists of only English letters (both uppercase and lowercase), digits (0-9
), plus'+'
, minus'-'
, or dot'.'
.
Valid Number– LeetCode Solutions
Valid Number in C++:
class Solution { public: bool isNumber(string s) { trim(s); if (s.empty()) return false; bool seenNum = false; bool seenDot = false; bool seenE = false; for (int i = 0; i < s.length(); ++i) { switch (s[i]) { case '.': if (seenDot || seenE) return false; seenDot = true; break; case 'e': if (seenE || !seenNum) return false; seenE = true; seenNum = false; break; case '+': case '-': if (i > 0 && s[i - 1] != 'e') return false; seenNum = false; break; default: if (!isdigit(s[i])) return false; seenNum = true; } } return seenNum; } private: void trim(string& s) { s.erase(0, s.find_first_not_of(' ')); s.erase(s.find_last_not_of(' ') + 1); } };
Valid Number in Java:
class Solution { public boolean isNumber(String s) { s = s.trim(); if (s.isEmpty()) return false; boolean seenNum = false; boolean seenDot = false; boolean seenE = false; for (int i = 0; i < s.length(); ++i) { switch (s.charAt(i)) { case '.': if (seenDot || seenE) return false; seenDot = true; break; case 'e': if (seenE || !seenNum) return false; seenE = true; seenNum = false; break; case '+': case '-': if (i > 0 && s.charAt(i - 1) != 'e') return false; seenNum = false; break; default: if (!Character.isDigit(s.charAt(i))) return false; seenNum = true; } } return seenNum; } }
Valid Number in Python:
class Solution: def isNumber(self, s: str) -> bool: s = s.strip() if not s: return False seenNum = False seenDot = False seenE = False for i, c in enumerate(s): if c == '.': if seenDot or seenE: return False seenDot = True elif c == 'e': if seenE or not seenNum: return False seenE = True seenNum = False elif c in '+-': if i > 0 and s[i - 1] != 'e': return False seenNum = False else: if not c.isdigit(): return False seenNum = True return seenNum