Given a string S(input consisting) of ‘*’ and ‘#’. The length of the string is variable.

Problem Statement – Given a string S(input consisting) of ‘*’ and ‘#’. The length of the string is variable. The task is to find the minimum number of ‘*’ or ‘#’ to make it a valid string. The string is considered valid if the number of ‘*’ and ‘#’ are equal. The ‘*’ and ‘#’ can be at any position in the string.
Note : The output will be a positive or negative integer based on number of ‘*’ and ‘#’ in the input string.

  • (*>#): positive integer
  • (#>*): negative integer
  • (#=*): 0

Example 1:
Input 1:

  • ###***   -> Value of S

Output :

  • 0   → number of * and # are equal

 

 Solution in C++

 

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s="Hello";
    int a=0,b=0;
    getline(cin,s);
    for(auto i:s)
        if(i=='#') 
            a++;
        else if(i=='*')
            b++;
    cout<<b-a;
}
 
 

 Solution in Java

 

import java.util.*;
public class Main
{
 	public static void main(String[] args)
 	{
        		
        String str="Hello";
        int count1=0,count2=0;
        for(int i=0;i< str.length();i++)
    	{
            if(str.charAt(i)=='*')
        		count1++;##
            else if(str.charAt(i)=='#')
         		count2++;
    		}
        System.out.println(count1-count2);
	}
}
 
 

 Solution in Python

 

s="Hello"
a=0
b=0
for i in s:
    if i=='*':
        a+=1
    elif i=='#':
        b+=1
print(a-b)

Comments

Popular posts from this blog

how to download and install secure exam browser for Accenture test

How to create Simple Company Website with Admin Panel in PHP | Free Source Code Download

SAP Labs Hiring Associate Developer (2022/2021 Batch)

Capgemini Engineering PSS hiring for-Engineering MCA Batch 2020/2021/2022

Amazon Hiring Cloud Support Associate (2022/2021 Batch)