Home > OS >  How to find substrings of length m that contain ith element(C#)
How to find substrings of length m that contain ith element(C#)

Time:07-12

We have a string length of n, I want to find all substrings of length m that contain ith element.

For example string s = "abcde"; I want to get all substrings of length 3 that contain 2nd element i.e. "b", in this case

abc, bcd

that is 2. But how we can code this for a general situation, i.e. substrings of length m, from a string of length n, that contain ith element

It would be good if substrings are stored in List<string>(not just Console.WriteLine())

CodePudding user response:

 public static List<string> GetSubStrings_i(string inputString, int mainIndex, int sbstrLength)
        {
            List<string> substringsList = new List<string>();


            for (int i = mainIndex - sbstrLength   1; i <= mainIndex; i  )
            {
                if (i < 0)
                    i = 0;

                if (i   sbstrLength <= inputString.Length)                                      
                    substringsList.Add(inputString.Substring(i, sbstrLength));
                
            }
                return substringsList;

        }

"*" is a symbol that all substrings should contain

Input

  • String = fsaf*faj;aijefaejsk
  • substrLength = 5
  • Index = 4

Output

  • substring = fsaf*

  • substring = saf*f

  • substring = af*fa

  • substring = f*faj

  • substring = *faj;

CodePudding user response:

Try This:

First you call getAllSubstrings, return list of all substrings of length m that contain i th element

List<String> matchsubstring = getAllSubstrings("abcde","b");

  public static List<String> getAllSubstrings(String inputString, String stringmatch)
  {
    List<String> resultList = new List<String>();
    
    for(int j = 0; j < inputString.Length; j  )
    {
        
        for(int i = 0; i < inputString.Length - j   1 ;i  )
        {
            if(inputString.Substring(i,j).Contains(stringmatch))
            {
                resultList.Add(inputString.Substring(i,j));
                Console.WriteLine("Substring Length : " j  
                        ", Substring :"   inputString.Substring(i,j)  
                        ", Index of string match :"  inputString.Substring(i,j).IndexOf(stringmatch.ToCharArray()[0]));
            }
        }
    }
    return resultList;
  }

Then you call getAllMatchSubstringByIndexing , return list of all substrings of length m that contain i th element at x index

List<String> outputList = getAllMatchSubstringByIndexing (matchsubstring ,"b",1)

matchsubstring is output of getAllSubstrings function

public static List<String> getAllMatchSubstringByIndexing(List<string> substring, String stringmatch, int index)
{
  List<String> substringWithIndex = new List<String>();
  foreach(var substringData in substring)
  {
      if(substringData.IndexOf(stringmatch.ToCharArray()[0]) == index)
      {
          substringWithIndex.Add(substringData);
          Console.WriteLine("Output : " substringData);
      }
  }
  return substringWithIndex;
}
  • Related