找出两个字符串数组中的相同元素
时间: 2023-09-04 17:15:33 浏览: 70
可以使用两个 for 循环来比较两个字符串数组中的元素,如果两个元素相同,则将其添加到一个新的数组中:
```python
def find_common(str1, str2):
common = []
for s1 in str1:
for s2 in str2:
if s1 == s2:
common.append(s1)
return common
```
这个函数接受两个字符串数组作为输入参数,它会返回一个新的数组,其中包含两个输入数组中相同的元素。
相关问题
javascript在两个文本中按字符长度从大到小找出相同字符串
以下是使用JavaScript实现的一种可能的解决方案:
```
function findCommonStrings(text1, text2) {
// 将文本转换为数组,每个元素代表一个单词
const words1 = text1.split(' ');
const words2 = text2.split(' ');
// 将单词按字符长度从大到小排序
words1.sort((a, b) => b.length - a.length);
words2.sort((a, b) => b.length - a.length);
// 初始化结果数组和指针
const result = [];
let i = 0;
let j = 0;
// 逐个比较单词,找出相同的
while (i < words1.length && j < words2.length) {
const word1 = words1[i];
const word2 = words2[j];
if (word1 === word2) {
result.push(word1);
i++;
j++;
} else if (word1.length > word2.length) {
i++;
} else {
j++;
}
}
return result;
}
// 使用示例
const text1 = 'JavaScript is a programming language that is used to create interactive effects on web pages.';
const text2 = 'Python is a high-level programming language for general-purpose programming.';
const commonStrings = findCommonStrings(text1, text2);
console.log(commonStrings); // ["programming", "language"]
```
该函数接受两个文本参数,将它们转换为单词数组,并按字符长度从大到小排序。然后,它使用两个指针逐个比较单词,找出相同的单词并将其添加到结果数组中。最后,它返回结果数组。在上面的示例中,它将找到两个文本中的“programming”和“language”单词。
定义两个等长字符串A和B的交叉操作,请用java给出示例,如下所示: 该操作的结果是与输入字符串长度相同的字符串result[i]是A[i]或B[i],当A[i]=B[i]时随机选择。 给定字符串数组inputArray和一个字符串result,从inputArray中找出多少对字符串,对它们进行交叉运算结果可能等于result。 注意:(A,B)和(B,A)是同一对。该对不能两次包含数组的相同元素(但是,如果数组中有两个相等的元素,它们可以形成一对)。即(3,3)只可被算作一对。
以下是一个Java实现:
```java
import java.util.*;
public class CrossString {
public static void main(String[] args) {
String[] inputArray = {"abc", "def", "abf", "deg"};
String result = "aef";
int count = countPairs(inputArray, result);
System.out.println(count);
}
public static int countPairs(String[] inputArray, String result) {
int count = 0;
Set<String> set = new HashSet<>();
for (int i = 0; i < inputArray.length; i++) {
for (int j = i + 1; j < inputArray.length; j++) {
String pair = inputArray[i] + "," + inputArray[j];
if (!set.contains(pair) && canCross(inputArray[i], inputArray[j], result)) {
count++;
set.add(pair);
}
}
}
return count;
}
private static boolean canCross(String A, String B, String result) {
if (A.length() != B.length() || A.length() != result.length()) {
return false;
}
char[] res = new char[result.length()];
for (int i = 0; i < result.length(); i++) {
if (A.charAt(i) == B.charAt(i)) {
if (result.charAt(i) == A.charAt(i)) {
res[i] = A.charAt(i);
} else if (result.charAt(i) == B.charAt(i)) {
res[i] = B.charAt(i);
} else {
res[i] = A.charAt(i);
}
} else {
if (result.charAt(i) == A.charAt(i)) {
res[i] = A.charAt(i);
} else if (result.charAt(i) == B.charAt(i)) {
res[i] = B.charAt(i);
} else {
Random random = new Random();
res[i] = random.nextBoolean() ? A.charAt(i) : B.charAt(i);
}
}
}
return new String(res).equals(result);
}
}
```
这个程序会输出在输入字符串数组中有多少对字符串可以进行交叉操作后得到与给定字符串result相同的结果。程序中使用了一个Set来避免重复计算,同时使用了canCross方法来判断两个字符串能否进行交叉操作后得到给定的结果。