Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Madhan has been a given a problem by his brother Jayi .The problem is related to strings.
Now he gives Madhan a long String S which consists of lowercase English Alphabets and wants him to tell the minimum size of substring such that all the substrings of that size in S have at least K Consonants(Non-vowels) in them.
If none of the subArray size satisfy the above condition ,return -1.

Input Format
Your function contains two arguments a String S and an Integer K.
First line of input contains a String S.
Second line of input contains an Integer K.

Constraints
1 <= |S| <= 100000
1 <= K <= 10000

Output Format
You must return an Integer denoting the answer to the problem.

Sample TestCase 1
Input:

ritikisagoodboy
4


Output:

9

Explanation

All substrings of length 9 have greater or equal to 4 non-vowels in them and this is the minimum size that we can have.
In substring of size 8 .. the substring ''ikisagoo'' has only 3 non-vowels therefore it violates the condition.


What I have tried:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class CandidateCode {

 static int Consonant(string input1,int input2)
    {
    	//code to be written 
    }

static void Main(String[] args) {
    int output;
    string ip1;
    ip1 = Console.ReadLine();
    int ip2;
    ip2 = Convert.ToInt32(Console.ReadLine());
    output = Consonant(ip1,ip2);
    Console.WriteLine(output);
    }

Could anyone please help me on solving this problem....
Posted
Updated 29-Nov-17 19:16pm

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
bapay 28-Feb-19 7:23am    
hi Permalink can you please clarify the problem . I don't even get that
OriginalGriff 28-Feb-19 7:31am    
I suspect you posted this in entirely the wrong place ...
This may help


public static int Consonant(String input1,int input2)
    {
       int consonant = 0;
       int total =-1;
       int count =0;
       String str="";
       int tot_substring =0;
  for (count=1;count<input1.length();count++)
  {
      int val=0;
      tot_substring=subStringCnt(input1,count);
    for (int j=0; j<input1.length(); j++) {
         consonant=0;
         str=input1.substring(j, Math.min(j + count, input1.length()));
          
         if(str.length()==count)
         {
             for (int i = 0; i < str.length(); i++) {
           if (isConsanant(str.charAt(i))) {
             consonant++;
             if (consonant >= input2){ val++; total=count;  break; }
              
           }
      }
     
    }
     
}
if(val==tot_substring) { break; }
}
  return total;
}
  public static boolean isConsanant(char c){
    String cons = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
    return cons.contains(Character.toString(c));
}
 
public static int subStringCnt(String str, int len)
{
    int cnt=0;
    String new_str="";
    for (int j=0; j<str.length(); j++) {
       new_str=str.substring(j, Math.min(j + len, str.length()));
        
       if(new_str.length()==len)
       {
            cnt++;
       }
     
}
return cnt;
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900