按正确顺序在字符串数组中查找常见字符 [英] Find common chars in array of strings, in the right order

查看:23
本文介绍了按正确顺序在字符串数组中查找常见字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几天时间研究一个函数,以正确的顺序获取字符串数组中的常见字符,以创建通配符.

I spent days working on a function to get common chars in an array of strings, in the right order, to create a wildcard.

这是一个解释我的问题的例子.我做了大约3个函数,但是每次字母绝对位置不同时总是有一个bug.

Here is an example to explain my problem. I made about 3 functions, but I always have a bug when the absolute position of each letter is different.

假设+"是通配符":

Array(
0 => '48ca135e0$5',
1 => 'b8ca136a0$5',
2 => 'c48ca13730$5',
3 => '48ca137a0$5');

应该返回:

$wildcard='+8ca13+0$5';

在这个例子中,棘手的是 $array[2] 比其他字符多 1 个字符.

In this example, the tricky thing is that $array[2] as 1 char more than others.

其他例子:

Array(
0 => "case1b25.occHH&FmM",
1 => "case11b25.occHH&FmM",
2 => "case12b25.occHH&FmM",
3 => "case20b25.occHH&FmM1");

应该返回:

$wildcard='case+b25.occHH&FmM+';

在这个例子中,棘手的部分是:
- 重复字符,例如删除"部分中的 1 -> 11,以及公共部分中的 c -> cc
- $array[2] & 中的2"字符[3]在删除"部分不在同一个位置
- 最后一个字符串末尾的1"字符

In this example, the tricky parts are :
- Repeating chars, such as 1 -> 11 in the "to delete" part, and c -> cc in the common part
- The "2" char in $array[2] & [3] in the "to delete" part is not in the same position
- The "1" char at the end of the last string

我真的需要帮助,因为我找不到此功能的解决方案,而且它是我应用程序的主要部分.

I really need help because I can't find a solution to this function and it is a main part of my application.

提前谢谢,不要犹豫,问问题,我会尽快回答.

Thanks in advance, don't hesitate to ask questions, I will answer as fast as possible.

Mykeul

推荐答案

主要代码:
步骤 1:将字符串按长度从最短到最长排序到数组[]
第 2 步:比较 array[0] 和 array[1] 中的字符串以获得 $temp_wildcard
第 3 步:将数组 [2] 中的字符串与 $temp_wildcard 进行比较以创建新的 $temp_wildcard
第 4 步:继续将每个字符串与 $temp_wildcard 进行比较 - 最后一个 $wildcard 是您的 $temp_wildcard

Main code:
Step 1: Sort strings by length, shortest to longest, into array[]
Step 2: Compare string in array[0] and array[1] to get $temp_wildcard
Step 3: Compare string in array[2] with $temp_wildcard to create new $temp_wildcard
Step 4: Continue comparing each string with $temp_wildcard - the last $wildcard is your $temp_wildcard

好的,现在我们来解决如何比较两个字符串以返回通配符字符串的问题.

OK, so now we're down to the problem of how to compare two strings to return your wildcard string.

子程序代码:逐个字符比较字符串,当比较不匹配时将通配符替换为返回值.

Subroutine code: Compare strings character-by-character, substituting wildcards into your return value when the comparison doesn't match.

为了处理不同长度的问题,对于第二个字符串较长并带有偏移量的每个字符,运行这个比较额外的时间.(比较 string1[x] 和 string2[x+offset].)对于每个返回的字符串,计算通配符的数量.子例程应该返回具有最少通配符字符数的答案.

To handle the problem of different lengths, run this comparison an extra time for each character that the second string is longer with an offset. (Compare string1[x] to string2[x+offset].) For each returned string, count the number of wildcard characters. The subroutine should return the answer with the fewest number of wildcard characters.

祝你好运!

这篇关于按正确顺序在字符串数组中查找常见字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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