对于给定的任何母串,一个子串出现的次数。
例如:
对于输入:母串 = "living in Shanghai", 子串"a" , 结果输出1
对于输入:母串 = "living in Shanghai", 子串"Sha" , 结果输出1
对于输入:母串 = "living in Shanghai", 子串"ha" , 结果输出2
对于输入:母串 = "living in Shanghai", 子串"aib" , 结果输出0
最简单的实现逻辑:
public static int CountSubString(string parent, string child) { int subStringCount = 0; int count = 0; for (int i = 0; i < parent.Length; i++) { int index = i; count = 0; for (int j = 0; j < child.Length; j++) { if (parent[i] == child[j]) { count++; if (i < parent.Length - 1) i = i + 1; } } if (count == child.Length) subStringCount++; if (i != index) i = index; } return subStringCount; }