Detect Capital - LeetCode Interview Coding Challenge [Java Brains]
ฝัง
- เผยแพร่เมื่อ 22 ส.ค. 2024
- In this video we will learn three different solutions and ways to look at what seems to be a very simple problem.
Interview Question: Given a word, you need to judge whether the usage of capitals in it is right or not.
Leetcode link: leetcode.com/p...
Difficulty level: Easy
Language: Java
Welcome to the Java Brains Java Interview challenge series of videos where we tackle various coding challenges ranging from the beginner level to advanced level - especially in the context of coding interviews.
Any coding problems you would like me to explore? Please let me know in the comments what you think!
#java #interview #javabrains
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++;}
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!
@Maxim Reed Instablaster :)
@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.
@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!
@Maxim Reed no problem :D
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;
}
u are literally using the loop twice due to toUpperCase and toLowerCase
Thanks alot, please continue the interview series.
Amazing
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;
}}
Please keep uploading videos on interview series.
Ok
Don't stop. Keep making this kind of videos.
Very Very Useful Video, Thanks a lot Koushik.
Hi, really entertaining video, I just wanted to mention with your solution you have one more iteration in case of two uppercase letters
Thanks for this serie of videos!
Great... please keep making this type of videos more...
Please keep uploading videos on leetcode.
Love this series.
What if i use regex and match the pattern , is it a good practice if no why ?
Amazing!!!!
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?
Well Explained ❤️
Thanks Koushik
I recommend javabrains to friends more than I recommend biryani 😛
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?
good solution
That's Awesome,one small suggestion Character.isUpperCase can be replaced with !isCorectCase.test
Thanks bro in advance.
Any way its going to be nice insightful video.
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.
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();
}
Will the solution mentioned in video takes care of string "fA" (first character lower case and second character upper case ) ? I dont think
wow.. thank you so much
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☺
can you explain a bit , how to use two pointer technique here?
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))
);
}
int check=1;
for (int i=1; i 1 && check != word.length())
System.out.println("Fail");
else
System.out.println("Pass");
Thanks for the explanation. But the Predicate solution still represents the O(n) time complexity, correct? Thanks in advance for response.
Yes
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.
Perfect
You don't have to say "I hope you found it helpful".ofcourse it will 👍
What if I used RegEx?
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;
}
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
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;
}
hii,
can you plz give me feedback on this solution is it good solution?
public static boolean checkCapital(String word){
if(word.length()
use sliding window technique
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;
}
You are working so hard to guide and help us...but also take care of yourself...looking sick.
You are the looking sick.
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));
}
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);