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 下批处理编程

    本文叫“ 初尝Windows 下批处理编程”是为了延续上一篇“初尝 Perl”,其实对于博主而言批处理以及批处理编程早就接触过了. 本文包括以下内容 1.什么是批处理 2.常用批处理命令 3.简介批处 ...

  2. hdu 1047 Integer Inquiry(高精度数)

    Problem Description Oneof the first users of BIT's new supercomputer was Chip Diller. He extended hi ...

  3. css部分基础归纳--学习笔记

    (1)css不区别大小写: (2)颜色值:颜色值可以写成RGB格式,如:color:rgb(255,100,0),也可以写成十六进制格式,如:color:#ff0000.如果十六进制的值是成对重复的可 ...

  4. RSA算法解析

    RSA算法原理(一) 如果你问我,哪一种算法最重要? 我可能会回答"公钥加密算法". 因为它是计算机通信安全的基石,保证了加密数据不会被破解.你可以想象一下,信用卡交易被破解的后果 ...

  5. Ms SQLServer中的Union和Union All的使用方法和区别

    Ms SQLServer中的Union和Union All的使用方法和区别 SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 ...

  6. 补充:学会Twitter Bootstrap不再难

    博客园的兄弟姐妹们很给力,自从这篇文章写出后,有人可能会对2.x版本升级到3.x版本的区别有些好奇和模糊.现在将官方给出的说明贴上去: 从2.x升级到3.0版本 Bootstrap 3并不向后兼容Bo ...

  7. python偏函数(functool.partail)

    functool.partail 方法可以为一个函数生成偏函数 import functools def f(a,b,c,d): print a,b,c,d a='a' b='b' f1=functo ...

  8. Custome Buble Data Point

    <navigation:Page xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/to ...

  9. 关于Weblogic连接池的TestConnectionOnReserve

        由于最近某客户的系统性能比较差,所以今天又上去跟踪了一下.看了一下Default Data Cache,发现已经从10G调整到了20G,所以可以确定应该是客户的管理员已经将双机从低配置的机器切 ...

  10. 【@Transactional】Spring 之注解事务 @Transactional

    spring 事务注解 默认遇到throw new RuntimeException("...");会回滚 需要捕获的throw new Exception("...&q ...