1 Star 0 Fork 65

欢喜派/C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
NaiveStringSearch.cs 1.44 KB
一键复制 编辑 原始数据 按行查看 历史
Andrii Siriak 提交于 2021-05-08 21:42 . Cleanup minor things (#221)
using System.Collections.Generic;
// Implements the traditional naive string matching algorithm in C# for TheAlgorithms/C-Sharp.
namespace Algorithms.Strings
{
/// <summary>
/// Implements the traditional naive string matching algorithm in C#.
/// </summary>
public static class NaiveStringSearch
{
/// <summary>
/// NaiveSearch(Content, Pattern) will return an array containing each index of Content in which Pattern appears.
/// Cost: O(n*m).
/// </summary>
/// <param name="content">The text body across which to search for a given pattern.</param>
/// <param name="pattern">The pattern against which to check the given text body.</param>
/// <returns>Array containing each index of Content in which Pattern appears.</returns>
public static int[] NaiveSearch(string content, string pattern)
{
var m = pattern.Length;
var n = content.Length;
List<int> indices = new();
for (var e = 0; e <= n - m; e++)
{
int j;
for (j = 0; j < m; j++)
{
if (content[e + j] != pattern[j])
{
break;
}
}
if (j == m)
{
indices.Add(e);
}
}
return indices.ToArray();
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/Joypai/C-Sharp.git
git@gitee.com:Joypai/C-Sharp.git
Joypai
C-Sharp
C-Sharp
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385