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. ios常用的一些类库

    在网上收集到的 一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地址:https:/ ...

  2. freemark页面中获取list循环中的counter

    如何在freemark页面中获取到当前list循环的counter 直接上代码 <#list lists as x> <#assign j=x?counter> ${j} // ...

  3. JAVA时钟

    效果图如下: //简单动态时钟程序,以图形和数字两种方式来显示当前时间 import javax.swing.*; import java.awt.event.*; import java.awt.* ...

  4. CentOS7 安装LAMP环境

    1.使用yum安装 yum -y install httpd mysql mysql-server php php-mysql postgresql postgresql-server php-pos ...

  5. Linux查找软件的安装路径

    软件安装的路径可能不止一个,可以使用whereis命令查看软件安装的所有路径,以mysql为例: whereis mysql 该命令会返回软件的所有安装路径: mysql: /usr/bin/mysq ...

  6. linux共享文件samba安装与java读取外部文件夹方法

    测试环境RedHat 6.4 一.安装 samba组件安装: (1)首先用“rpm –qa |grep samba”命令检验系统samba服务是否安装. #rpm –qa |grep samba sa ...

  7. winform 通过 html 与swf 交互 简单案例

    在上一篇 winform 与 html 交互 简单案例 中讲了winform与html之间的简单交互,接下来的内容是在winform中以html为中转站,实现将swf嵌入winform中并实现交互. ...

  8. TeeChart 的应用

    TeeChart 是一个很棒的绘图控件,不过由于里面没有注释,网上相关的资料也很少,所以在应用的时候只能是一点点的试.为了防止以后用到的时候忘记,我就把自己用到的东西都记录下来,以便以后使用的时候查询 ...

  9. NetBeans中文乱码解决办法

    一.Windows下NetBeans中文乱码解决办法 找到你的Netbeans安装目录下的etc文件夹,用记事本打开netbeans.conf,找到netbeans_default_options(不 ...

  10. javascipt学习笔记1

    一.javascript 部分 1.整理 <<javascript>> 要学习哪些章节 及核心内容? ①javascript简介 核心技术点:javascript定义 作用特点 ...