[C#]与元音和辅音有关的问题 [英] [C#] problem related to vowels and consonants

查看:131
本文介绍了[C#]与元音和辅音有关的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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....

推荐答案

我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但我们不打算为你做这一切!
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!


这可能会有所帮助





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


这篇关于[C#]与元音和辅音有关的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆