How many substring are balanced

Code examples

0
0

import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Scanner;
 import java.util.Set;

    public class BalancedStrings {
    static int  returnbalance(String input){
    HashMap<Character, Integer> characters=new LinkedHashMap<>();
    int count=0;
    Character c=null;
    for(int i=0;i<input.length();i++){
        c=input.charAt(i);
        if(characters.containsKey(c)==true){
            count=characters.get(c);
            characters.put(c, count+1);
        }
        else
            characters.put(c,1);        
    }
    int countunique=0;
    Set<Character> s=characters.keySet();       
    for(Character cc:s){            
        count=characters.get(cc);
        if(count%2==0)
            countunique++;
    }
    if(countunique!=s.size())
        return 0;
    Object[] sar= s.toArray();
    int len=0;
    for(int i=sar.length-1;i>=0;i--){
        len=len+i;
    }

    return len+countunique;

}
public static void main(String[] args) {
    Scanner in=new Scanner(System.in);
    int numberOfInputs=in.nextInt();
    in.nextLine();
    for(int i=0;i<numberOfInputs;i++){
        System.out.println(BalancedStrings.returnbalance(in.nextLine()));
    }

}

}

Similar pages

Similar pages with examples

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................