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=...

Introduction to Reactive Programming in Java with Project Reactor

Introduction to Reactive Programming in Java with Project Reactor we will go through below: 1. Introduction to Reactive Programming What is reactive programming? Key principles of reactive systems: Responsive : Systems should respond in a timely manner. Resilient : Systems should be fault-tolerant. Elastic : Systems should scale as needed. Message-driven : Systems should use asynchronous messaging. Comparison between imperative programming and reactive programming. 2. Understanding Reactive Streams Publisher , Subscriber , Subscription , and Processor interfaces. The four key signals: onNext() , onComplete() , onError() , and onSubscribe() . Backpressure handling in reactive systems. 3. Introduction to Project Reactor What is Project Reactor? Key classes: Mono and Flux . Mono : Represents 0 or 1 item. Flux : Represents 0 to N items. Non-blocking nature and how it helps in building scalable systems. 4. Building a Reactive Application with Project Reactor Demonstrating how to use Mono ...

Password Keeper - Secret Diary - Offline Password keeper - Secret Manager - MySecrets -PNSoft - password manager

You might be having account in many banks or other websites for which you cannot remember passwords, there’s simply no way to easily remember every single password without duplicating passwords. This is where a password manager comes in—as long as you create a strong master password that you can remember, that’s the last password you’ll need to deal with. One Key to ALL Locks :) Data breaches are caused by weak or reused passwords. To keep your information secure, you need a strong, unique password for every account. But when you have hundreds of accounts, how do you keep track of all your passwords? That’s where password managers come in. They help you create one master password which will keep your all your passwords.  MySecrets -PNSoft   is a  free and password manager  primarily for  Windows .  MySecrets -PNSoft stores usernames, passwords, and other fields, including free-form notes in an encrypted  file . This file can be protected b...