1 Star 0 Fork 0

jobily/TheAlgorithms-C-Sharp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
NaiveStringSearch.cs 1.31 KB
一键复制 编辑 原始数据 按行查看 历史
Gerson Jr 提交于 2024-01-05 20:45 . Switch to file-scoped namespaces (#433)
using System.Collections.Generic;
// Implements the traditional naive string matching algorithm in C# for TheAlgorithms/C-Sharp.
namespace Algorithms.Strings.PatternMatching;
/// <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 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hubo/the-algorithms-c-sharp.git
git@gitee.com:hubo/the-algorithms-c-sharp.git
hubo
the-algorithms-c-sharp
TheAlgorithms-C-Sharp
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385