Detect Capital - LeetCode Interview Coding Challenge [Java Brains]

Sdílet
Vložit
  • čas přidán 6. 09. 2024

Komentáře • 53

  • @tejucb
    @tejucb Před 3 lety +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;
    }}

  • @azharshaikh5055
    @azharshaikh5055 Před 4 lety +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 Před 2 lety +1

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

  • @ArindamKar
    @ArindamKar Před 4 lety +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 Před 3 lety

      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 Před 3 lety

      @Maxim Reed Instablaster :)

    • @maximreed9029
      @maximreed9029 Před 3 lety

      @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 Před 3 lety

      @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 Před 3 lety

      @Maxim Reed no problem :D

  • @samahmahdi5511
    @samahmahdi5511 Před měsícem

    Thanks alot, please continue the interview series.

  • @sujan_kumar_mitra
    @sujan_kumar_mitra Před 4 lety +7

    Please keep uploading videos on interview series.

  • @I_am_Stardust_97
    @I_am_Stardust_97 Před 3 lety +1

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

  • @ranjitdesai3926
    @ranjitdesai3926 Před rokem

    Amazing

  • @Giorgi.Japiashvili
    @Giorgi.Japiashvili Před rokem

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

  • @mrinalsharma4137
    @mrinalsharma4137 Před 3 lety +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?

  • @mirageman2
    @mirageman2 Před 4 lety +1

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

  • @arkasingha6111
    @arkasingha6111 Před 3 lety

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

  • @KtrVisionaryLeader
    @KtrVisionaryLeader Před 4 lety

    Very Very Useful Video, Thanks a lot Koushik.

  • @sekharch2918
    @sekharch2918 Před rokem

    Will the solution mentioned in video takes care of string "fA" (first character lower case and second character upper case ) ? I dont think

  • @trmontenegro
    @trmontenegro Před rokem

    Thanks for this serie of videos!

  • @sahukarinaveenkumar3188

    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 Před 4 lety

      can you explain a bit , how to use two pointer technique here?

  • @zheeshut5498
    @zheeshut5498 Před 4 lety

    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 Před 4 lety

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

  • @TheSumitSagar
    @TheSumitSagar Před 4 lety

    Please keep uploading videos on leetcode.
    Love this series.

  • @dineshshekhawat2021
    @dineshshekhawat2021 Před 3 lety +1

    What if I used RegEx?

  • @nathansnow
    @nathansnow Před 3 lety

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

  • @ashimasood6531
    @ashimasood6531 Před 4 lety

    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.

  • @abhilashmund
    @abhilashmund Před 4 lety +2

    I recommend javabrains to friends more than I recommend biryani 😛

  • @areebhussain5639
    @areebhussain5639 Před 3 lety

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

  • @sambit8011
    @sambit8011 Před 4 lety

    Well Explained ❤️
    Thanks Koushik

  • @abhijitmadchetti5443
    @abhijitmadchetti5443 Před 4 lety

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

  • @venkataramanan2381
    @venkataramanan2381 Před 4 lety

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

  • @eshagupta9407
    @eshagupta9407 Před 3 lety

    Amazing!!!!

  • @sagarmeena0210
    @sagarmeena0210 Před 3 lety

    good solution

  • @stewargeovany
    @stewargeovany Před rokem

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

  • @truth-seeker-2300
    @truth-seeker-2300 Před 4 lety +1

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

    • @gaureesha9840
      @gaureesha9840 Před 4 lety +1

      Yes

    • @IvanRandomDude
      @IvanRandomDude Před 3 lety +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.

  • @sindhusudhakaran1731
    @sindhusudhakaran1731 Před 3 lety

    wow.. thank you so much

  • @narendra9903
    @narendra9903 Před 3 lety +1

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

  • @Anubis10110
    @Anubis10110 Před 4 lety

    Perfect

  • @Giorgi.Japiashvili
    @Giorgi.Japiashvili Před rokem

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

  • @FaizanShaikh-pp1kg
    @FaizanShaikh-pp1kg Před 3 lety

    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 Před 4 lety

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

  • @anil2009
    @anil2009 Před 4 lety

    use sliding window technique

  • @hardikjoshi4
    @hardikjoshi4 Před 3 lety

    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 Před 4 lety

    You are working so hard to guide and help us...but also take care of yourself...looking sick.

  • @cjimenez2581
    @cjimenez2581 Před rokem

    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 Před 2 lety

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