Which are in?

Given two arrays of strings a1 and a2 return a sorted array in lexicographical order and without duplicates of the strings of a1 which are substrings of strings of a2.

Example: a1 = ["arp", "live", "strong"]

a2 = ["lively", "alive", "harp", "sharp", "armstrong"]

returns ["arp", "live", "strong"]

a1 = ["tarp", "mice", "bull"]

a2 = ["lively", "alive", "harp", "sharp", "armstrong"]

returns []

Note: Arrays are written in "general" notation. See "Your Test Cases" for examples in your language.

using System;
using System.Linq;
using System.Collections.Generic; class WhichAreIn
{
public static string[] inArray(string[] array1, string[] array2)
{
// your code
List<string> list = new List<string>();
foreach (string strItem1 in array1)
{
foreach (string strItem2 in array2)
{
if (strItem2.Contains(strItem1))
{
if (list.Contains(strItem1) == false)
{
list.Add(strItem1);
}
}
}
}
list = list.OrderBy(x => x).ToList() ;
return list.ToArray();
}
}

其他人的解法:

需要注意distinct的用法,以及any的用法

using System;
using System.Linq; class WhichAreIn
{
public static string[] inArray(string[] array1, string[] array2)
{
return array1.Distinct()
.Where(s1 => array2.Any(s2 => s2.Contains(s1)))
.OrderBy(s => s)
.ToArray();
}
}

随机推荐

  1. Windows Forms(二)

    导读 1.用VS创建一个Windows Forms程序 2.分析上面的程序 3.Mediator pattern(中介者模式) 4.卡UI怎么办——BackgroundWorker组件 用VS创建一个 ...

  2. “Assign Random Colors” is not working in 3ds Max 2015

    Go to Customize -> Preferences…-> General (tab) Uncheck “Default to By Layer for New Nodes”

  3. Java标准输入输出流的重定向及恢复

    在Java中输入输出数据一般(图形化界面例外)要用到标准输入输出流System.in和System.out,System.in,System.out默认指向控制台,但有时程序从文件中输入数据并将结果输 ...

  4. OpenJudge 2774 木材加工

    1.链接: http://bailian.openjudge.cn/practice/2774/ 2.题目: 总Time Limit: 1000ms Memory Limit: 65536kB Des ...

  5. CIFS与NFS(转)

    1.CIFS Microsoft推出SMB(server message block)后,进一步发展,使其扩展到Internet上,成为common internet file system. CIF ...

  6. gentoo下grub文件编辑

    在编译完内核,配置好网络,配置好fstab文件等等,最后一个至关重要的文件要属grub文件了,该文件的配置成功才最终决定gentoo 是否成功装上,首先当然是 emerge grub 了,现在就可以配 ...

  7. Yii 配置默认controller和action

    设置默认controller 在/protected/config/main.php添加配置 <?php return array( 'name'=>'Auto', 'defaultCon ...

  8. codeforces 630K - Indivisibility

    K. Indivisibility 题意:给一个n(1 <= n <= 10^18)的区间,问区间中有多少个数不能被2~10这些数整除: 整除只需要看素数即可,只有2,3,5,7四个素数: ...

  9. js 拼接 三列做为一行

    function Ajax_GetCourseAndResource(data) { $(".ol-course-list").empty(); var html = " ...

  10. Java之向左添加零(000001)第二种方法

    //待测试数据 int i = 100; //得到一个NumberFormat的实例 NumberFormat nf = NumberFormat.getInstance(); //设置是否使用分组 ...