Java Interview Question: How To Count Occurrences Of Each Character In String In Java

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ส.ค. 2021
  • In this video, I have explained one famous Java Interview Question: How To Count Occurrences Of Each Character In String In Java
    ~~~Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:
    th-cam.com/users/Naveen%20Au...
    Follow me on my Facebook Page:
    / naveenqtpexpert
    Let's join our Automation community for some amazing knowledge sharing and group discussion on Telegram:
    t.me/joinchat/Q8HTanc9Xi4os0tS
    Naveen AutomationLabs Paid Courses:
    Java & Selenium:
    naveenautomationlabs.com/manua...
    Java & API +POSTMAN + RestAssured + HttpClient:
    naveenautomationlabs.com/selen...
  • วิทยาศาสตร์และเทคโนโลยี

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

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

    All my interest in Java is because of you. I can't thank you enough for all that you have done for us!! 👏

  • @surabhibn541
    @surabhibn541 2 ปีที่แล้ว +1

    Your approach is really so easy to understand and you solve the program in most efficient way too
    Thank you so much...

  • @lavanya.ds0657
    @lavanya.ds0657 3 ปีที่แล้ว +1

    Thank you souch for sharing this Naveen, this is mandatory question asked in many interviews for experienced candidates

  • @909praveen
    @909praveen 3 ปีที่แล้ว +8

    Thanks Naveen for the video . For the assessment we just have to remove not symbol from if(String.valueOf(c).is blank()).

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

    Thank you Naveen!! God bless you 🙏

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

    Thankyou sir the way you explained awesome..it's makes every one to easy understand...
    For only spaces count we use instead of not if condition
    if(String.valueOf(ch).isBank())

  • @khajazakiuddin931
    @khajazakiuddin931 2 ปีที่แล้ว +1

    Thank you so much for the amazing videos on Java concepts Naveen. it really helps in improving my coding skills. Could you also please make a video on how to print all the permutations of a given string "abc" (like acb, bca, cab, cba..etc.,). (It was asked in the interview with ADP company). There are many logics present on the internet but not fully understandable, just wanted to get it from you which obviously would be easy to understand and implement.

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

    Thank you so much naveen😍

  • @NagarajuN-ev3pl
    @NagarajuN-ev3pl 3 หลายเดือนก่อน

    Hi NAVEEN,
    Good day!
    The same program I was facing in the technical round and I wrote and explained to the interviewer. I cracked and cleared the first round as well. Thank you, please post more mock interviews. Thanks a lot..

  • @varshamishra8743
    @varshamishra8743 2 ปีที่แล้ว

    Thank you Sir.. Nice video 👍😊

  • @vijaykanthperikala
    @vijaykanthperikala 2 ปีที่แล้ว

    Hi Naveen, I'm getting below error when I try to use "is Blank()" method.
    "The method isBlank() is undefined for the type String"
    Could you please help me ?
    I'm using jdk 1.8.0-301 version

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

    Very nice video Naveen

  • @13success66
    @13success66 ปีที่แล้ว

    Beautiful explanation

  • @arunkumar-ke3km
    @arunkumar-ke3km 2 วันที่ผ่านมา

    veryuseful video,

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

    Best approach

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

    Thanks Naveen.

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

    Thanks!

  • @anantpandey309
    @anantpandey309 2 ปีที่แล้ว

    Hi Naveen, I m getting error while using the isBlank() method. Error showing that method isBlank() is undefined for type string.

  • @VivekKumar-bu6ho
    @VivekKumar-bu6ho ปีที่แล้ว

    Sir, how can i get output as te2s3h if input is teesssh with stream ??

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

    veryyy very nice explaination

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

    Solution for only space we need to take count of string and replaceall method
    int n=name.length()-name.replaceAll(" ", "").length();
    System.out.println("spaceCount");
    System.out.println(n);

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

    YOU'RE THE MAN!!, THANK YOU!!!!!!!!!!!! but why u had to test ur code for 10 min lol

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

    Thanks for the explanation Naveen. It's because of you, yesterday I cracked one more interview!

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

    Why did you give only hash map? And how to choose which map to use in which scenario?

  • @ParamjitKaur-jo2sz
    @ParamjitKaur-jo2sz ปีที่แล้ว +1

    To count only spaces, we will remove NOT from condition like below
    if(String.valueOf(c).isBlank())

  • @devanandthirunavukkarasu5148
    @devanandthirunavukkarasu5148 2 ปีที่แล้ว

    sir. thanks for the video .can we do this in java 8

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

    Make a video for String Builder and String Buffer bro.

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

    most of the interview this question asked ...if not this then ...some what similar to this problem.

  • @haribabuvelakavaram5935
    @haribabuvelakavaram5935 2 ปีที่แล้ว

    Hi Naveen,
    Just a question..
    For thr first test case i.e input "test" the output Hashmap values must be {(t,1)(e,1)(s,1)(t,2)} .. right??
    Because we are just adding a new key value pair to the hashmap but we are not updating the existing key-value pairs as per this code...
    For the 4th input character : 't' it will check if 't' is already present in the map. Since it is already there, it will fetch it's value which is 1 and then increments it by 1..so it will become 2 and will update a new key-value pair as( t,2) to the existing hashmap...so the final hashmap output should be {(t,1)(e,1)(s,1)(t,2)} ..
    Please explain how it is giving {(e,1)(s,1)(t,2)} as output..
    Thanks in advance.

    • @SurajSharma-fw5fg
      @SurajSharma-fw5fg ปีที่แล้ว

      Because he is using HashMap and HashMap can not contain duplicate "key". Hence, only one 't' will be there in HashMap.

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

    solution for counting space
    int count=0;
    for(char ch:name.toCharArray()) {
    if(ch==' ') {
    count++;
    }
    }
    System.out.println("******************");
    System.out.println(count);

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

    Print occurences of each word and character in a string : it was asked in the interview

  • @aparna5088
    @aparna5088 5 หลายเดือนก่อน

    This program is case sensitive
    the uppercase and lowercase occurences of same letters are counted seperately.

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

    I am well versed in Python, Should I learn Java as well?

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

      Yes if you trying for Selenium. Selenium with java is the most demanding as compared to other language bindings.

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

    simple remove ! in (String.valueOf(C).isBlank()){} condtion

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

    String S1="hello"
    String S2="12345"
    Output=h1e2l3l4o5. Can you plz write program for above output

    • @lalitjadaunreaction014
      @lalitjadaunreaction014 7 หลายเดือนก่อน

      bhai simple hai 1 loop creta karlo then bhs ek character fist string sai or 2nd character from 2 string using charAt[i]. simple sa trika to ye hai . or bhi efficienct way

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

    sir ap hindi main bnao yr video

  • @saikumar-io7un
    @saikumar-io7un 2 ปีที่แล้ว

    Better, u can do videos for Less time

  • @STUDIES-wv4mw
    @STUDIES-wv4mw 7 หลายเดือนก่อน

    public static void spaceCount(String str) {
    int count = 0;
    char[] c = str.toCharArray();
    for (char ce : c) {
    if (ce == ' ') {
    count++;
    }
    }
    System.out.println("Space Count " + count);
    }

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

    Hello Naveen thanks for this video..
    One interview que...if u can try for us...
    String str[]={"Suresh:20","Ramesh:10","Anand:05"}
    We want to sort the str array on the basis of age....

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

      String str[]={"Suresh:20","Ramesh:10","Anand:05"};
      List list= new ArrayList();
      for(String s:str){
      list.add(Integer.parseInt(s.substring(str[0].indexOf(":")+1)));
      }
      Collections.sort(list);
      String str2[]=new String[str.length];
      for(int i=0;i

  • @codingsskills.explore
    @codingsskills.explore 2 ปีที่แล้ว

    1 2 three 3 4 three 5 6 three 7 8 three.......20.anyone can solve this please aisa output aana chahiye

  • @utkarshpatil2674
    @utkarshpatil2674 7 หลายเดือนก่อน

    Answer to the assignment, please do correct otherwise
    import java.util.HashMap;
    import java.util.Map;
    public class charCount {

    public static void getCharCount(String name) {

    Map charMap=new HashMap();

    char strArray[]=name.toCharArray();

    for(char c: strArray) {
    if(String.valueOf(c).contains(" "))
    {
    if(charMap.containsKey(c)) {
    charMap.put(c , charMap.get(c)+1 );
    }
    else {
    charMap.put(c, 1);
    }
    }
    }
    System.out.println(name + " : " + charMap);
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    getCharCount("sel sel sel");


    }
    }

  • @ravi54217
    @ravi54217 2 ปีที่แล้ว +1

    public class CharCount {
    public static void main(String[] args) {
    System.out.println(countBy(" This is a string ", Character::isSpaceChar));
    }
    private static long countBy(final String string, final IntPredicate condition) {
    return string.chars().filter(condition).count();
    }
    }

  • @programminghubbharat7535
    @programminghubbharat7535 2 ปีที่แล้ว

    Yes sir, by just removing the (!) In the
    if(!string.valueof(c).isBlank()) we got only spaces