Skip to main content

Count substring with equal number of 0s 1s and 2s

Here given code implementation process.

/*
   Java Program
   Count substring with equal number of 0s 1s and 2s
*/
import java.util.HashMap;
import javafx.util.Pair;
public class Substring
{
	// Count substring of equal number of zeros,ones and twos
	public void equalCounter(String text)
	{
		// Get the number of characters
		int n = text.length();
		int ans = 0;
		// Define the counter variables
		int zero = 0;
		int one = 0;
		int two = 0;
		HashMap < Pair < Integer, Integer > , Integer > record = 
          new HashMap < Pair < Integer, Integer > , Integer > ();
		// First pair
		record.put(new Pair < Integer, Integer > (0, 0), 1);
		// iterate the loop through by text length
		for (int i = 0; i < n; i++)
		{
			if (text.charAt(i) == '0')
			{
				zero++;
			}
			else if (text.charAt(i) == '1')
			{
				one++;
			}
			else if (text.charAt(i) == '2')
			{
				two++;
			}
			else
			{
				System.out.print("Invalid char : " + text.charAt(i));
				return;
			}
			// Create new pair
			Pair < Integer, Integer > p = new Pair < Integer, Integer > (zero - one, zero - two);
			if (record.containsKey(p))
			{
				// Get new result 
				ans = ans + record.get(p);
				// increase pairing pairs
				record.put(p, record.get(p) + 1);
			}
			else
			{
				// Add new pair
				record.put(p, 1);
			}
		}
      	System.out.println("  Text  : " + text);
		System.out.println(" Result : " + ans);
	}
	public static void main(String[] arg)
	{
		Substring task = new Substring();
		String text = "100102021021001102";
		/*
		    // Single character
		    100102021021001102
		       102
		    100102021021001102
		          021
		    100102021021001102
		           210
		    100102021021001102
		            102
		    100102021021001102
		             021
		    100102021021001102
		              210
		    100102021021001102
		                   102
		    // Double character
		    100102021021001102
		       102021
		    100102021021001102
		          021021
		    100102021021001102
		           210210
		    // Three character
		    100102021021001102
		       102021021
		*/
		task.equalCounter(text);
	}
}

Output

  Text  : 100102021021001102
 Result : 11
// Include header file
#include <iostream>
#include <utility> 
#include <string>
#include <map>

using namespace std;
/*
   C++ Program
   Count substring with equal number of 0s 1s and 2s
*/
class Substring
{
	public:
		// Count substring of equal number of zeros,ones and twos
		void equalCounter(string text)
		{
			// Get the number of characters
			int n = text.length();
			int ans = 0;
			// Define the counter variables
			int zero = 0;
			int one = 0;
			int two = 0;
			map < pair < int, int > , int > record ;
			// First pair
			record[make_pair(0, 0)] = 1;
			// iterate the loop through by text length
			for (int i = 0; i < n; i++)
			{
				if (text[i] == '0')
				{
					zero++;
				}
				else if (text[i] == '1')
				{
					one++;
				}
				else if (text[i] == '2')
				{
					two++;
				}
				else
				{
					cout << "Invalid char : " << text[i];
					return;
				}
				pair < int, int > p = make_pair(zero - one, zero - two);
				if (record.find(p) != record.end())
				{
					// Get new result
					ans = ans + record[p];
					// increase pairing pairs
					record[p] = record[p] + 1;
				}
				else
				{
					// Add new pair
					record[p] = 1;
				}
			}
			cout << "  Text  : " << text;
			cout << "\n Result : " << ans;
		}
};
int main()
{
	Substring task = Substring();
	string text = "100102021021001102";
	/*
	    // Single character
	    100102021021001102
	       102
	    100102021021001102
	          021
	    100102021021001102
	           210
	    100102021021001102
	            102
	    100102021021001102
	             021
	    100102021021001102
	              210
	    100102021021001102
	                   102
	    // Double character
	    100102021021001102
	       102021
	    100102021021001102
	          021021
	    100102021021001102
	           210210
	    // Three character
	    100102021021001102
	       102021021
	*/
	task.equalCounter(text);
	return 0;
}

Output

  Text  : 100102021021001102
 Result : 11
// Include namespace system
using System;
using System.Collections.Generic;
/*
   C# Program
   Count substring with equal number of 0s 1s and 2s
*/
public class Substring
{
	// Count substring of equal number of zeros,ones and twos
	public void equalCounter(String text)
	{
		// Get the number of characters
		int n = text.Length;
		int ans = 0;
		// Define the counter variables
		int zero = 0;
		int one = 0;
		int two = 0;
		Dictionary < Tuple < int, int > , int > record = 
          new Dictionary < Tuple < int, int > , int > ();
		// First pair
		record.Add(Tuple.Create(0,0), 1);
		// iterate the loop through by text length
		for (int i = 0; i < n; i++)
		{
			if (text[i] == '0')
			{
				zero++;
			}
			else if (text[i] == '1')
			{
				one++;
			}
			else if (text[i] == '2')
			{
				two++;
			}
			else
			{
				Console.Write("Invalid char : " + text[i]);
				return;
			}
			// Create new pair
			Tuple <int,int> p = Tuple.Create(zero - one, zero - two);
			if (record.ContainsKey(p))
			{
				// Get new result
				ans = ans + record[p];
				// increase pairing pairs
				record[p] =  record[p] + 1;
			}
			else
			{
				// Add new pair
				record.Add(p, 1);
			}
		}
		Console.WriteLine("  Text  : " + text);
		Console.WriteLine(" Result : " + ans);
	}
	public static void Main(String[] arg)
	{
		Substring task = new Substring();
		String text = "100102021021001102";
		/*
		    // Single character
		    100102021021001102
		       102
		    100102021021001102
		          021
		    100102021021001102
		           210
		    100102021021001102
		            102
		    100102021021001102
		             021
		    100102021021001102
		              210
		    100102021021001102
		                   102
		    // Double character
		    100102021021001102
		       102021
		    100102021021001102
		          021021
		    100102021021001102
		           210210
		    // Three character
		    100102021021001102
		       102021021
		*/
		task.equalCounter(text);
	}
}

Output

  Text  : 100102021021001102
 Result : 11
#   Python 3 Program
#   Count substring with equal number of 0s 1s and 2s

class Substring :
	#  Count substring of equal number of zeros,ones and twos
	def equalCounter(self, text) :
		#  Get the number of characters
		n = len(text)
		ans = 0
		#  Define the counter variables
		zero = 0
		one = 0
		two =  0
		record = dict()
		#  First pair
		record[(0,0)] = 1
		#  iterate the loop through by text length
		for  i in range(n)  :
			if (text[i] == '0') :
				zero += 1
			
			elif(text[i] == '1') :
				one += 1
			
			elif(text[i] == '2') :
					two += 1
			else :
				print("Invalid char : ", text[i], end = "")
				return
				
			#  Create new pair
			p = (zero - one, zero - two)
			if (p in record.keys()) :
				#  Get new result
				ans = ans + record.get(p)
				#  increase pairing pairs
				record[p] = record.get(p) + 1
			else :
				#  Add new pair
				record[p] = 1
	
		print("  Text  : ", text)
		print(" Result : ", ans)


def main() :
	task = Substring()
	text = "100102021021001102"
	# 
	#     // Single character
	#     100102021021001102
	#        102
	#     100102021021001102
	#           021
	#     100102021021001102
	#            210
	#     100102021021001102
	#             102
	#     100102021021001102
	#              021
	#     100102021021001102
	#               210
	#     100102021021001102
	#                    102
	#     // Double character
	#     100102021021001102
	#        102021
	#     100102021021001102
	#           021021
	#     100102021021001102
	#            210210
	#     // Three character
	#     100102021021001102
	#        102021021
	
	task.equalCounter(text)

if __name__ == "__main__": main()

Output

  Text  :  100102021021001102
 Result :  11




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment