Which are in?
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();
}
}
随机推荐
- 关于四字节字符入库时错误的解决方案(Incorrect string value: '\xF0\x9F\x99\x8F' for column 'Reply_Content' at row 1)
1. 将表字段字符集设置成utf8mb4 2. 执行插入前执行:SET NAMES utf8mb4; 如: SET NAMES utf8mb4; INSERT test(Content) VALUES ...
- 使用SQL Server 2005 新的语法ROW_NUMBER()进行分页的两种不同方式的性能比较
相比在SQL Server 2000 中使用的分页方式,在SQL Server 2005中使用新的语法ROW_NUMBER()来分页效率要高出很多,但是很多人在使用ROW_NUMBER()这种分页方式 ...
- 初涉GitHub
安装 访问https://help.github.com/articles/set-up-git/,选择对应OS平台.有文档参考,我的是OpenSuse. 在console中下载安装http://ww ...
- DataGridView绘制序号
1.找到RowPostPaint事件 2.写入事件 /// <summary> /// 绘制序号 /// </summary> private void dgvStatemen ...
- c#基础知识对比(面向对象)
private,protected,public和internal private:是完全私有的,只有本类自己能用[好比自己的老婆,只有你自己可以调用,其他谁都不可以] protected:可被外界看 ...
- DataGridView几个基本属性
DataGridView 经常用到,但是很多东西都不熟悉,以至于总去上网查,这次我整理一下,全部都记下来,再用就方便了. 1.禁止用户新建行,就是去掉最后那个行标题上带星号的那个行 dataGridV ...
- mac下如何查看指定端口被谁占用并且杀死该进程
在本地部署 Web 应用时我有遇到过某网络端口已经被其他程序占用的情况,这时候就需要先退出占用该端口的进程,我们可以通过“终端”来实现结束占用某特定端口的进程 1.打开终端,使用如下命令: lsof ...
- 使用Github总结
1. 使用Git GUI 首先熟悉一下GUI,如下: 第一步,首先将代码fork到自己的版本库下面,如下: 并获取clone URL,如下图: 然后点击GUI克隆已有版本库,如下图: 点击克隆就可以得 ...
- hdu 3336 Count the string KMP+DP优化
Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...
- Oracle目录结构及创建新数据库
oracle目录结构 当需要创建新的数据仓库时我可以用 Database Configuration Assistant(数据库配置助手) admin 存放创建的不同数据库 cfgtoollogs c ...