Search
 
SCRIPT & CODE EXAMPLE
 

CPP

how many substring are balanced

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

}

}
Comment

no of balanced substrings

#include <bits/stdc++.h> 
 using namespace std; 
 int dp[100005]; 
 int main() 
 { 
       int t,n; 
      long long ans; 
       string s; 
       map <int,long long> m; 
      cin >> t; 
      while ( t-- ) 
     { 
              cin >> s; 
             n = (int)s.size();//calculates length of string 
              assert(n<=100000);//checks if n<=100000 
              m.clear(); 
              dp[0] = 0; 
             ans = 0; 
              for ( int i = 1; i <= n; i++ )  
                     dp[i] = dp[i-1]^(1<<(s[i-1]-97)); 
              m[dp[0]]++; 
              for ( int i = 1; i <= n; i++ ) 
             { 
                     ans += m[dp[i]]; 
                    m[dp[i]]++; 
             } 
            cout << ans << endl; 
      } 
      return 0; 
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ graphics online compiler 
Cpp :: warning: base will be initialized after 
Cpp :: vector int initialize with increasing numbers 
Cpp :: how to type a vertical stack program c++ 
Cpp :: cplusplusbtutotrail 
Cpp :: #include <iostream #include <stdio.h using namespace std; int main() { int a[5]; a[0]=12; a[1]=13; a[2]=14; a[3]=15; 
Cpp :: nmake.exe is not found in the windows 
Cpp :: initalising array c++ 
Cpp :: C++ Modified Data Types List 
Cpp :: get range sum 
Cpp :: PascalName seperate strings 
Cpp :: class how to call main method inheritance in c++ 
Cpp :: argument to number C++ 
Cpp :: C++ Multilevel Inheritance 
Cpp :: array di struct 
Cpp :: stl map 
Cpp :: std::copy 
Cpp :: CPP Find options passed from command line 
Cpp :: how to add values in empty array using python 
Cpp :: sort using comparator anonymous function c++ 
Cpp :: segment tree lazy propogation 
Cpp :: float to byte array and back c++ with memcpy command 
Cpp :: how to refresh multiple command lines in C++ stream 
Cpp :: convert c program to c++ online 
Cpp :: ranged based for loop c++ 
Cpp :: c++ rgb code 
Cpp :: no argument but return value in c++ 
Cpp :: pycuda install failed microsoft c++ 
Cpp :: c++ vector merge algorithm 
Cpp :: 1672. Richest Customer Wealth leetcode solution in c++ 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =