Skip to main content

Count each character and sort the string

Here we have shown how to count the character occurrence in the string and print in sorted way




For example, you have a string :
input : bbcann
and your output would be 1b2c1n2 , means you have counted and also printed in sorted way from a to z.
This is commonly asked questions on online test.

public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        System.out.print(countChracter("bbcann"));
    }
    
    
   static String countChracter(String s){
    char[] carray=s.toCharArray();
    Arrays.sort(carray);
    HashMap<Character,Integer> hm = new HashMap<Character, Integer>();
    for(char c : carray){
    if(hm.containsKey(c)){
    hm.put(c, hm.get(c)+1);
    }else{
    hm.put(c, 1);
    }
   
    }
    String output="";
    for(Map.Entry e: hm.entrySet()){
    output=output+e.getKey()+e.getValue();
    }
    return output;
    }
    
}



Comments

Popular posts from this blog

Camunda Spring Boot with Shared Engine with War file

This is the only one website (may be first) to show an example to create working war file for camunda shared engine using Spring Boot. We analysed the problem using pure spring boot "starter" dependencies that it contains camunda engine plus tomcat, so even when you remove tomcat using either <provided> or <exclude>, it continues to run on tomcat as its already with in camunda engine. And when you try to create war file and paste in into shared camunda engine outside then that war will no work. This is because that war you created containing camunda engine and a tomcat inside it and when you paste into another outside camunda engine, it gets confused to which camunda to use. Hence I worked on this and brought a mix version of spring and spring boot implementation of camunda. So here you go....and thanks me later :) Here is the pom file -------------------------------------------------------------------------------------------- <?xml version=...

Stream flatMap() in Java with examples

Stream flatMap() in Java with examples Stream flatMap(Function mapper) returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Stream flatMap(Function mapper) is an  intermediate operation . These operations are always lazy. Intermediate operations are invoked on a Stream instance and after they finish their processing, they give a Stream instance as output. Note :  Each mapped stream is closed after its contents have been placed into this stream. If a mapped stream is null, an empty stream is used, instead. flatMap() V/s map()  : 1) map() takes a Stream and transform it to another Stream. It applies a function on each element of Stream and store return value into new Stream. It does not flatten the stream. But flatMap() is the combination of a map and a flat operation i.e, it applies a function to elements as well as f...

Hibernate interview questions

Question : Why JPA Entity or Hibernate Persistence Class Should Not be Final?  Answer : One of the interesting hibernate interview questions is, why you should not make a Hibernate persistent class final? I'll try to answer this question in this short blog post. The use of proxies is the core feature of Hibernate (one of the most popular ORM frameworks for Java Projects) for implementing key performance features e.g. lazy loading and lazy associations fetching. In order to use a proxy in place of a real class, your hibernate persistence class must be either non-final or the implementation of an interface that declares all of the public methods. Difference between save vs persist and saveOrUpdate in Hibernate Save vs. saveOrUpdate vs. persist in Hibernate? What is the difference between save and saveOrUpdate or Difference between saving and persist are common interview question in any Hibernate interview, much like the difference between get and load method in Hibernate. H...