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

ความคิดเห็น • 53

  • @ArindamKar
    @ArindamKar 4 ปีที่แล้ว +5

    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

  • @azharshaikh5055
    @azharshaikh5055 3 ปีที่แล้ว +7

    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 ปีที่แล้ว +1

      u are literally using the loop twice due to toUpperCase and toLowerCase

  • @samahmahdi5511
    @samahmahdi5511 26 วันที่ผ่านมา

    Thanks alot, please continue the interview series.

  • @ranjitdesai3926
    @ranjitdesai3926 ปีที่แล้ว

    Amazing

  • @tejucb
    @tejucb 3 ปีที่แล้ว +3

    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;
    }}

  • @sujan_kumar_mitra
    @sujan_kumar_mitra 4 ปีที่แล้ว +7

    Please keep uploading videos on interview series.

  • @arkasingha6111
    @arkasingha6111 3 ปีที่แล้ว

    Don't stop. Keep making this kind of videos.

  • @HydInfra
    @HydInfra 4 ปีที่แล้ว

    Very Very Useful Video, Thanks a lot Koushik.

  • @mirageman2
    @mirageman2 4 ปีที่แล้ว +1

    Hi, really entertaining video, I just wanted to mention with your solution you have one more iteration in case of two uppercase letters

  • @trmontenegro
    @trmontenegro ปีที่แล้ว

    Thanks for this serie of videos!

  • @ziaulhaq6971
    @ziaulhaq6971 4 ปีที่แล้ว

    Great... please keep making this type of videos more...

  • @TheSumitSagar
    @TheSumitSagar 4 ปีที่แล้ว

    Please keep uploading videos on leetcode.
    Love this series.

  • @I_am_Stardust_97
    @I_am_Stardust_97 3 ปีที่แล้ว +1

    What if i use regex and match the pattern , is it a good practice if no why ?

  • @eshagupta9407
    @eshagupta9407 3 ปีที่แล้ว

    Amazing!!!!

  • @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?

  • @sambit8011
    @sambit8011 4 ปีที่แล้ว

    Well Explained ❤️
    Thanks Koushik

  • @abhilashmund
    @abhilashmund 4 ปีที่แล้ว +2

    I recommend javabrains to friends more than I recommend biryani 😛

  • @mrinalsharma4137
    @mrinalsharma4137 3 ปีที่แล้ว +1

    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?

  • @sagarmeena0210
    @sagarmeena0210 3 ปีที่แล้ว

    good solution

  • @abhijitmadchetti5443
    @abhijitmadchetti5443 4 ปีที่แล้ว

    That's Awesome,one small suggestion Character.isUpperCase can be replaced with !isCorectCase.test

  • @venkataramanan2381
    @venkataramanan2381 4 ปีที่แล้ว

    Thanks bro in advance.
    Any way its going to be nice insightful video.

  • @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.

  • @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();
    }

  • @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

  • @sindhusudhakaran1731
    @sindhusudhakaran1731 3 ปีที่แล้ว

    wow.. thank you so much

  • @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?

  • @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))
    );
    }

  • @areebhussain5639
    @areebhussain5639 3 ปีที่แล้ว

    int check=1;
    for (int i=1; i 1 && check != word.length())
    System.out.println("Fail");
    else
    System.out.println("Pass");

  • @truth-seeker-2300
    @truth-seeker-2300 4 ปีที่แล้ว +1

    Thanks for the explanation. But the Predicate solution still represents the O(n) time complexity, correct? Thanks in advance for response.

    • @gaureesha9840
      @gaureesha9840 4 ปีที่แล้ว +1

      Yes

    • @IvanRandomDude
      @IvanRandomDude 3 ปีที่แล้ว +1

      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.

  • @Anubis10110
    @Anubis10110 3 ปีที่แล้ว

    Perfect

  • @narendra9903
    @narendra9903 3 ปีที่แล้ว +1

    You don't have to say "I hope you found it helpful".ofcourse it will 👍

  • @dineshshekhawat2021
    @dineshshekhawat2021 3 ปีที่แล้ว +1

    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;
    }

  • @FaizanShaikh-pp1kg
    @FaizanShaikh-pp1kg 3 ปีที่แล้ว

    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

  • @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;
    }

  • @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()

  • @anil2009
    @anil2009 4 ปีที่แล้ว

    use sliding window technique

  • @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;
    }

  • @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);