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++;}
@maximreed90293 жыл бұрын
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!
@jaxamari49013 жыл бұрын
@Maxim Reed Instablaster :)
@maximreed90293 жыл бұрын
@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.
@maximreed90293 жыл бұрын
@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!
@jaxamari49013 жыл бұрын
@Maxim Reed no problem :D
@samahmahdi55113 ай бұрын
Thanks alot, please continue the interview series.
@sujan_kumar_mitra4 жыл бұрын
Please keep uploading videos on interview series.
@abdullahkunhi46894 жыл бұрын
Ok
@mirageman24 жыл бұрын
Hi, really entertaining video, I just wanted to mention with your solution you have one more iteration in case of two uppercase letters
@tejucb4 жыл бұрын
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 Жыл бұрын
Thanks for this serie of videos!
@HyderabadInfra4 жыл бұрын
Very Very Useful Video, Thanks a lot Koushik.
@arkasingha61113 жыл бұрын
Don't stop. Keep making this kind of videos.
@mrinalsharma41374 жыл бұрын
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?
@TheSumitSagar4 жыл бұрын
Please keep uploading videos on leetcode. Love this series.
@zheeshut54984 жыл бұрын
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?
@ziaulhaq69714 жыл бұрын
Great... please keep making this type of videos more...
@sahukarinaveenkumar31884 жыл бұрын
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☺
@SpiritOfIndiaaa4 жыл бұрын
can you explain a bit , how to use two pointer technique here?
@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 Жыл бұрын
Amazing
@ashimasood65314 жыл бұрын
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.
@abhijitmadchetti54434 жыл бұрын
That's Awesome,one small suggestion Character.isUpperCase can be replaced with !isCorectCase.test
@truth-seeker-23004 жыл бұрын
Thanks for the explanation. But the Predicate solution still represents the O(n) time complexity, correct? Thanks in advance for response.
@gaureesha98404 жыл бұрын
Yes
@IvanRandomDude3 жыл бұрын
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.
@sindhusudhakaran17313 жыл бұрын
wow.. thank you so much
@abhilashmund4 жыл бұрын
I recommend javabrains to friends more than I recommend biryani 😛
@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_973 жыл бұрын
What if i use regex and match the pattern , is it a good practice if no why ?
@ShinAkuma2 ай бұрын
regex is really slow.
@eshagupta94074 жыл бұрын
Amazing!!!!
@narendra99033 жыл бұрын
You don't have to say "I hope you found it helpful".ofcourse it will 👍
@sagarmeena02104 жыл бұрын
good solution
@venkataramanan23814 жыл бұрын
Thanks bro in advance. Any way its going to be nice insightful video.
@azharshaikh50554 жыл бұрын
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; }
@aryamanpande54992 жыл бұрын
u are literally using the loop twice due to toUpperCase and toLowerCase
@sambit80114 жыл бұрын
Well Explained ❤️ Thanks Koushik
@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; }
@dineshshekhawat20214 жыл бұрын
What if I used RegEx?
@nathansnow3 жыл бұрын
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; }
@Anubis101104 жыл бұрын
Perfect
@FaizanShaikh-pp1kg4 жыл бұрын
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
@praveenj31124 жыл бұрын
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; }
@areebhussain56393 жыл бұрын
int check=1; for (int i=1; i 1 && check != word.length()) System.out.println("Fail"); else System.out.println("Pass");
hii, can you plz give me feedback on this solution is it good solution? public static boolean checkCapital(String word){ if(word.length()
@amargupta17284 жыл бұрын
You are working so hard to guide and help us...but also take care of yourself...looking sick.
@codegeek82564 жыл бұрын
You are the looking sick.
@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)); }
@saurabhjoshi45602 жыл бұрын
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);