Detect Capital - LeetCode Interview Coding Challenge [Java Brains]

  Рет қаралды 26,940

Java Brains

Java Brains

Күн бұрын

Пікірлер: 54
@ArindamKar
@ArindamKar 4 жыл бұрын
Nicely explained and neat solution with predicate. Btw for the smart solution, if we add one more check in the if block inside for loop, we can skip checking whole word and return on first invalid combination : if( i != numberOfCapitals) { return false; } else{ numberOfCapitals++;}
@maximreed9029
@maximreed9029 3 жыл бұрын
you all probably dont care but does anybody know a way to get back into an Instagram account?? I somehow lost the password. I would appreciate any help you can give me!
@jaxamari4901
@jaxamari4901 3 жыл бұрын
@Maxim Reed Instablaster :)
@maximreed9029
@maximreed9029 3 жыл бұрын
@Jax Amari Thanks so much for your reply. I found the site through google and I'm in the hacking process now. Takes quite some time so I will get back to you later with my results.
@maximreed9029
@maximreed9029 3 жыл бұрын
@Jax Amari It did the trick and I actually got access to my account again. I'm so happy! Thank you so much, you saved my account!
@jaxamari4901
@jaxamari4901 3 жыл бұрын
@Maxim Reed no problem :D
@samahmahdi5511
@samahmahdi5511 3 ай бұрын
Thanks alot, please continue the interview series.
@sujan_kumar_mitra
@sujan_kumar_mitra 4 жыл бұрын
Please keep uploading videos on interview series.
@abdullahkunhi4689
@abdullahkunhi4689 4 жыл бұрын
Ok
@mirageman2
@mirageman2 4 жыл бұрын
Hi, really entertaining video, I just wanted to mention with your solution you have one more iteration in case of two uppercase letters
@tejucb
@tejucb 4 жыл бұрын
If we can use java String: private boolean validate(String inputWord) { String substring = inputWord.substring(1); if (inputWord.toUpperCase().equals(inputWord) || substring.toLowerCase().equals(substring)) { return true; } return false; }}
@trmontenegro
@trmontenegro Жыл бұрын
Thanks for this serie of videos!
@HyderabadInfra
@HyderabadInfra 4 жыл бұрын
Very Very Useful Video, Thanks a lot Koushik.
@arkasingha6111
@arkasingha6111 3 жыл бұрын
Don't stop. Keep making this kind of videos.
@mrinalsharma4137
@mrinalsharma4137 4 жыл бұрын
Why the loop starts from 1 again when we know both the characters are upper case? I mean shouldn't it start from 2 for the the case where all letters should be capital?
@TheSumitSagar
@TheSumitSagar 4 жыл бұрын
Please keep uploading videos on leetcode. Love this series.
@zheeshut5498
@zheeshut5498 4 жыл бұрын
Explained it so well. Thank you sir. You make every Leet code solutions so easy to understand. Just wanted to know if there is any way to read the entire data on RAM, if there is kindly let's know how to do it?
@ziaulhaq6971
@ziaulhaq6971 4 жыл бұрын
Great... please keep making this type of videos more...
@sahukarinaveenkumar3188
@sahukarinaveenkumar3188 4 жыл бұрын
Instead of using for loop, we can use two pointer technique for better efficiency. case: JavabrainS in this case last letter is capital, if we use two pointer technique we can return false in the first check only☺
@SpiritOfIndiaaa
@SpiritOfIndiaaa 4 жыл бұрын
can you explain a bit , how to use two pointer technique here?
@sekharch2918
@sekharch2918 Жыл бұрын
Will the solution mentioned in video takes care of string "fA" (first character lower case and second character upper case ) ? I dont think
@ranjitdesai3926
@ranjitdesai3926 Жыл бұрын
Amazing
@ashimasood6531
@ashimasood6531 4 жыл бұрын
Great video and cannot thank you enough for your amazing work. I have a small doubt with the lambda solution. For the second if where we check that the first and second characters are uppercase, the common for loop checks again the same thing for the second character ( int i = 1). Would it make sense to have a local variable which we can assign to i? Initially it is 1 and if the second if is encountered we change it to 2, to avoid checking character at position 2 twice.
@abhijitmadchetti5443
@abhijitmadchetti5443 4 жыл бұрын
That's Awesome,one small suggestion Character.isUpperCase can be replaced with !isCorectCase.test
@truth-seeker-2300
@truth-seeker-2300 4 жыл бұрын
Thanks for the explanation. But the Predicate solution still represents the O(n) time complexity, correct? Thanks in advance for response.
@gaureesha9840
@gaureesha9840 4 жыл бұрын
Yes
@IvanRandomDude
@IvanRandomDude 3 жыл бұрын
Of course. This problem cannot be faster than O(n). You simply have to check every character in order to determine if the word is valid. You can't get around that no matter what you try, you can just make it more elegant and fancy but not faster.
@sindhusudhakaran1731
@sindhusudhakaran1731 3 жыл бұрын
wow.. thank you so much
@abhilashmund
@abhilashmund 4 жыл бұрын
I recommend javabrains to friends more than I recommend biryani 😛
@Giorgi.Japiashvili
@Giorgi.Japiashvili Жыл бұрын
Also it can be done like this public static boolean oldestDetectCapitalUse(String word) { return word.toLowerCase().equals(word) || word.toUpperCase().equals(word) || (Character.isUpperCase(word.charAt(0)) && word.substring(1).toLowerCase().equals( word.substring(1)) ); }
@I_am_Stardust_97
@I_am_Stardust_97 3 жыл бұрын
What if i use regex and match the pattern , is it a good practice if no why ?
@ShinAkuma
@ShinAkuma 2 ай бұрын
regex is really slow.
@eshagupta9407
@eshagupta9407 4 жыл бұрын
Amazing!!!!
@narendra9903
@narendra9903 3 жыл бұрын
You don't have to say "I hope you found it helpful".ofcourse it will 👍
@sagarmeena0210
@sagarmeena0210 4 жыл бұрын
good solution
@venkataramanan2381
@venkataramanan2381 4 жыл бұрын
Thanks bro in advance. Any way its going to be nice insightful video.
@azharshaikh5055
@azharshaikh5055 4 жыл бұрын
This can be done without loop. We can use toUpperCase() and toLowerCase() of String. if(word.toUpperCase().equals(word)) { return true; } else if(word.toLowerCase().equals(word)) { return true; } else if(Character.isUpperCase(word.charAt(0)) && word.substring(1).toLowerCase().equals(word.substring(1))) { return true; } else { return false; }
@aryamanpande5499
@aryamanpande5499 2 жыл бұрын
u are literally using the loop twice due to toUpperCase and toLowerCase
@sambit8011
@sambit8011 4 жыл бұрын
Well Explained ❤️ Thanks Koushik
@Giorgi.Japiashvili
@Giorgi.Japiashvili Жыл бұрын
I did it using XOR operator (^): public static boolean detectCapitalUse(String word) { if (word.length() > 1 && !Character.isUpperCase(word.charAt(0)) && Character.isUpperCase(word.charAt(1))) return false; for (int i = 2; i < word.length(); i++) { if (Character.isUpperCase(word.charAt(i)) ^ Character.isUpperCase(word.charAt(i - 1))) return false; } return true; }
@dineshshekhawat2021
@dineshshekhawat2021 4 жыл бұрын
What if I used RegEx?
@nathansnow
@nathansnow 3 жыл бұрын
Or without any loops.... static boolean detectCapital(String s){ //check tail upper case if(s.substring(0, s.length()).compareTo( s.substring(0, s.length()).toUpperCase()) == 0){ //head must be upper case if(s.substring(0,1).compareTo( s.substring(0,1).toUpperCase()) == 0) return true; } //check if tail all lower case (head irrelevant) return s.substring(1, s.length()).compareTo( s.substring(1, s.length()).toLowerCase()) == 0; }
@Anubis10110
@Anubis10110 4 жыл бұрын
Perfect
@FaizanShaikh-pp1kg
@FaizanShaikh-pp1kg 4 жыл бұрын
Is below code a better solution? private static boolean detectCapitalUse(String s) { boolean flag= false; if(s.equals(s.toUpperCase()) || s.equals(s.toLowerCase())) { flag=true; }else if(Character.isUpperCase(s.charAt(0))){ int count=0; for(int i=1;i
@praveenj3112
@praveenj3112 4 жыл бұрын
Hi Koushik, Accepted solution in the Leetcode : public boolean detectCapitalUse(String word) { String str=new String(word); if(word.equals(str.toUpperCase())) return true; if(word.equals(str.toLowerCase())) return true; if(Character.isUpperCase(word.charAt(0)) && word.substring(1).equals(str.substring(1).toLowerCase())) return true; return false; }
@areebhussain5639
@areebhussain5639 3 жыл бұрын
int check=1; for (int i=1; i 1 && check != word.length()) System.out.println("Fail"); else System.out.println("Pass");
@stewargeovany
@stewargeovany Жыл бұрын
Using: import java.util.regex.Pattern; public boolean detectCapital(String word) { Pattern textPattern = Pattern.compile("[a-z]*|[A-Z]*|^[A-Z][a-z]*"); return textPattern.matcher(word).matches(); }
@anil2009
@anil2009 4 жыл бұрын
use sliding window technique
@hardikjoshi4
@hardikjoshi4 3 жыл бұрын
hii, can you plz give me feedback on this solution is it good solution? public static boolean checkCapital(String word){ if(word.length()
@amargupta1728
@amargupta1728 4 жыл бұрын
You are working so hard to guide and help us...but also take care of yourself...looking sick.
@codegeek8256
@codegeek8256 4 жыл бұрын
You are the looking sick.
@cjimenez2581
@cjimenez2581 Жыл бұрын
Too much code... public static boolean detectCapitalUseV2(String word) { int numCapitals = 0; boolean foundLower = false; for (int i = 0; i < word.length(); i++) { if (Character.isUpperCase(word.charAt(i))) { numCapitals++; }else { foundLower = true; } if(foundLower && numCapitals > 1) { return false; } } if (numCapitals == 0 || numCapitals == word.length()) { return true; } return numCapitals == 1 && Character.isUpperCase(word.charAt(0)); }
@saurabhjoshi4560
@saurabhjoshi4560 2 жыл бұрын
just one check without for loop for each word... String input="The Farmer WAS watching ALl the fiELDs @"; Map output=new LinkedHashMap(); String[] wordArray=input.split(" "); for(String tmp:wordArray){ if((tmp.toUpperCase().equals(tmp)) // check if all are caps || (tmp.toLowerCase().equals(tmp) ) // check if all are small || ((tmp.charAt(0)==tmp.toUpperCase().charAt(0)) // check if only first is caps && (tmp.substring(1).toLowerCase().equals(tmp.substring(1))))) output.put(tmp, " VALID Word"); else output.put(tmp, " IN VALID Word"); } System.out.println("Output "+output);
ТЮРЕМЩИК В БОКСЕ! #shorts
00:58
HARD_MMA
Рет қаралды 2,6 МЛН
Увеличили моцареллу для @Lorenzo.bagnati
00:48
Кушать Хочу
Рет қаралды 8 МЛН
5 interview techniques to show problem solving skills
17:23
Java Brains
Рет қаралды 11 М.
My Brain after 569 Leetcode Problems
7:50
NeetCode
Рет қаралды 2,7 МЛН
Top 25 Microservice Interview Questions Answered - Java Brains
39:54
ТЮРЕМЩИК В БОКСЕ! #shorts
00:58
HARD_MMA
Рет қаралды 2,6 МЛН