I went and cheeked the same issue in leet code, and was able to fix it without a for-loop just validate if the string was null or ends with e, or + or . then try to cast it to decimal, if casting failed, exit with false (it worked) if (s == "Infinity" || s == "-Infinity" || s == "+Infinity") return false; ... more validations try: double num = double.Parse(s); catch: return false;
You can solve it with a regex and e counter (I'm new to CS and from mobile so it's pretty bad): Bool isValidDecimal(string str) { Regex reg = /[-+]?\d+(\.\d+?(e -? \d+))?/; Return reg.test(str) ; } [-+]? A sign in the start and its optional \d+ must be at least one digit before the decimal point The parentheses and question sign mean that all the part of decimal point is option \. We're escaping the dot because Normally dot in regex means every character and now it means just dot \d+? There might be numbers before the exponent (e -? \d+)? There might be not exponent and if there is there must be just one and after the decimal point and afterwards might be a minus sign and there must be at least one digit I really didn't think about it that much and there might be some cases I missed and rook me forever ro type cuz of mobile so correct me if I'm wrong, I'm also only 5 months into coding so yeah
Accepted. class Solution: def isNumber(self, s: str) -> bool: s = s.lower() try: if 'inf' in s: return False x = float(s) return True except: return False Runtime: 32 ms, faster than 79.16% of Python3 online submissions for Valid Number. Memory Usage: 14.1 MB, less than 84.88% of Python3 online submissions for Valid Number.
Hi Guys! I may not be professional in writing code. But I solved it within 2 minutes. I used Python's Exception Handling concept here, by float(input string) if it gives value error just return false. Trust me It really worked 😂😂😂
this problem is not so hard, it is just very poorly written and you don't know what is considered valid and what not until you have attempted it many times. I had to attempt it nine times, and each time I found out a new edge case what is considered valid or not valid, until the tenth time I finally got it.
there is already a function that checks this problem but here an implementation on C #include #include #include bool is_decimal_number(const char *str) { if (str == NULL) { return false; } // Check if the string is empty if (*str == '\0') { return false; } // Check if the string starts with a negative sign if (*str == '-') { str++; } // Check if the string contains only digits while (*str != '\0') { if (!isdigit(*str)) { return false; } str++; } return true; } int main(void) { const char *str1 = "123"; const char *str2 = "-123"; const char *str3 = "123.45"; const char *str4 = "abc"; printf("%d ", is_decimal_number(str1)); // prints 1 printf("%d ", is_decimal_number(str2)); // prints 1 printf("%d ", is_decimal_number(str3)); // prints 0 printf("%d ", is_decimal_number(str4)); // prints 0 return 0; }
If anyone wants to study together or make a new friend, feel free to dm me! It would be awesome to go over any programming concepts or Leetcode together!
You explain this so well that a beginner can inplement this into his/her starter language
Difficult problem easily explained, great job and kudos for this.
I would definitely say that you have understood the problem thoroughly and thanks for letting us know the way you understood. Thanks mate.
I appreciate that!
you explain really well, good job! Thanks for the vidéo :)
Glad it was helpful!
Another hard problem made easy!
great explanation!
Thank you so much!
I went and cheeked the same issue in leet code, and was able to fix it without a for-loop
just validate if the string was null or ends with e, or + or .
then try to cast it to decimal, if casting failed, exit with false (it worked)
if (s == "Infinity" || s == "-Infinity" || s == "+Infinity") return false;
... more validations
try:
double num = double.Parse(s);
catch:
return false;
Haha nice
Awesome explanation.. keep up the good work.
Thank you very much!
You can solve it with a regex and e counter (I'm new to CS and from mobile so it's pretty bad):
Bool isValidDecimal(string str) {
Regex reg = /[-+]?\d+(\.\d+?(e -? \d+))?/;
Return reg.test(str) ;
}
[-+]? A sign in the start and its optional
\d+ must be at least one digit before the decimal point
The parentheses and question sign mean that all the part of decimal point is option
\. We're escaping the dot because Normally dot in regex means every character and now it means just dot
\d+? There might be numbers before the exponent
(e -? \d+)? There might be not exponent and if there is there must be just one and after the decimal point and afterwards might be a minus sign and there must be at least one digit
I really didn't think about it that much and there might be some cases I missed and rook me forever ro type cuz of mobile so correct me if I'm wrong, I'm also only 5 months into coding so yeah
Regex is a great way to solve it, nice!
Regex would be cheating. Is like the interviewer asking you to implement any sorting algorithm and you just use a library.
why not just trim then regex?
Cause I'm bad with regex lol
Accepted.
class Solution:
def isNumber(self, s: str) -> bool:
s = s.lower()
try:
if 'inf' in s:
return False
x = float(s)
return True
except:
return False
Runtime: 32 ms, faster than 79.16% of Python3 online submissions for Valid Number.
Memory Usage: 14.1 MB, less than 84.88% of Python3 online submissions for Valid Number.
Hi Guys! I may not be professional in writing code. But I solved it within 2 minutes. I used Python's Exception Handling concept here, by float(input string) if it gives value error just return false. Trust me It really worked 😂😂😂
Thank you! I learnt the real way to solve this problem. 😊
Hahaha nice
Anytime!
I did same. I did it in seconds
@@akxyn5010 Great 😀
is it better if you use regex?
this problem is not so hard, it is just very poorly written and you don't know what is considered valid and what not until you have attempted it many times.
I had to attempt it nine times, and each time I found out a new edge case what is considered valid or not valid, until the tenth time I finally got it.
Yep, exactly. That's why the problem has so many downvotes haha
Really good breakdown
Thanks!
there is already a function that checks this problem but here an implementation on C
#include
#include
#include
bool is_decimal_number(const char *str) {
if (str == NULL) {
return false;
}
// Check if the string is empty
if (*str == '\0') {
return false;
}
// Check if the string starts with a negative sign
if (*str == '-') {
str++;
}
// Check if the string contains only digits
while (*str != '\0') {
if (!isdigit(*str)) {
return false;
}
str++;
}
return true;
}
int main(void) {
const char *str1 = "123";
const char *str2 = "-123";
const char *str3 = "123.45";
const char *str4 = "abc";
printf("%d
", is_decimal_number(str1)); // prints 1
printf("%d
", is_decimal_number(str2)); // prints 1
printf("%d
", is_decimal_number(str3)); // prints 0
printf("%d
", is_decimal_number(str4)); // prints 0
return 0;
}
Which website was that?
Leetcode.com :)
why not just convert it to parseInt or parseDouble and handle exceptions to check instead?
You can do that, but I imagine in an interview they might restrict you to not be able to use those functions.
(constant space complexity)
“Memory Usage: less than 6.25% of submissions”
da fuq everybody doin over there
lol right, sometimes I feel like people are hard coding the test cases haha
regular expression is good way
If anyone wants to study together or make a new friend, feel free to dm me! It would be awesome to go over any programming concepts or Leetcode together!
Nice explain, but i still not understand 😅 iwant to learn coding but, i hope i can 😅
It takes alot of time, just make sure to be consistent!
Regardless of difficulty let us agree it is just a bad coding question.
100% a terrible question lol