Partial Word Searching

Write a method that will search an array of strings for all strings that contain another string, ignoring capitalization. Then return an array of the found strings.

The method takes two parameters, the query string and the array of strings to search, and returns an array.

If the string isn‘t contained in any of the strings in the array, the method returns an array containing a single string: "Empty".

Example: If the string to search for is "me", and the array to search is ["home", "milk", "Mercury", "fish"], the method should return ["home", "Mercury"].

import java.util.Arrays; class WordSearch { static String[] findWord(String x, String[] y){ final String xLower = x.toLowerCase(); String[] searchResults = Arrays.stream(y) .filter(s -> s.toLowerCase().contains(xLower)) .toArray(size -> new String[size]); return searchResults.length > 0 ? searchResults : new String[] {"Empty"}; } }

class WordSearch { static String[] findWord(String x, String[] y) { List<String> found = new ArrayList<>(); x = x.toLowerCase(); for(String str : y) { if(str.toLowerCase().indexOf(x) > -1) { found.add(str); } } return found.size() > 0 ? found.toArray(new String[0]) : new String[]{"Empty"}; } }

class WordSearch { static String[] findWord(String x, String[] y){ ArrayList<String> sols = new ArrayList<String>(); for(int i = 0; i < y.length; i++) { if(y[i].toLowerCase().contains(x.toLowerCase())){ sols.add(y[i]); } } String[] empty = {"Empty"}; return sols.isEmpty() ? empty : sols.toArray(new String[sols.size()]); } }

时间: 2025-01-03 21:02:24

Partial Word Searching的相关文章

man bash

BASH(1) General Commands Manual BASH(1) NAME bash - GNU Bourne-Again SHell SYNOPSIS bash [options] [command_string | file] COPYRIGHT Bash is Copyright (C) 1989-2013 by the Free Software Foundation, Inc. DESCRIPTION Bash is an sh-compatible command la

Java BitSet使用场景和示例

一.什么是BitSet? 注:以下内容来自JDK API: BitSet类实现了一个按需增长的位向量.位Set的每一个组件都有一个boolean值.用非负的整数将BitSet的位编入索引.可以对每个编入索引的位进行测试.设置或者清除.通过逻辑与.逻辑或和逻辑异或操作,可以使用一个 BitSet修改另一个 BitSet的内容. 默认情况下,set 中所有位的初始值都是false. 每个位 set 都有一个当前大小,也就是该位 set 当前所用空间的位数.注意,这个大小与位 set 的实现有关,所以

Autocomplete TEdit

http://forum.codecall.net/topic/75946-autocomplete-tedit/ Overview Autocomplete feature really helpful for us speeding up our typing job. For you who is not familiar with the term autocomplete, it's when you type partial part of a word and then you w

BNU 27847——Cellphone Typing——————【字典树】

Cellphone Typing Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 1252664-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next Type: None None Graph Theory 2-SAT A

Cellphone Typing 字典树

Cellphone Typing Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 1252664-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next Font Size: + - Type:   None Graph Th

[email&#160;protected] java bit自我practice##Q&amp;A:为何int 来初始化size of bitVector?long,甚至是BigInteger等策略

/* * BitSets are packed into arrays of "words." Currently a word is * a long, which consists of 64 bits, requiring 6 address bits. * The choice of word size is determined purely by performance concerns. */ private final static int ADDRESS_BITS_P

BERT论文解读

本文尽量贴合BERT的原论文,但考虑到要易于理解,所以并非逐句翻译,而是根据笔者的个人理解进行翻译,其中有一些论文没有解释清楚或者笔者未能深入理解的地方,都有放出原文,如有不当之处,请各位多多包含,并希望得到指导和纠正. 论文标题 Bert:Bidirectional Encoder Representations from Transformers 一种从Transformers模型得来的双向编码表征模型. 论文地址 https://arxiv.org/pdf/1810.04805 Abstr

我的VSTO之路(四):深入介绍Word开发

原文:我的VSTO之路(四):深入介绍Word开发 在上一篇文章中,我介绍了Word的对象模型和一些基本开发技巧.为了更好的介绍Word插件开发,我为本文制作了一个Word书签的增强版,具体功能是让用户在Word中选择一段文本,为它添加书签并其标志为高亮,同时用户可以为这段书签写注释,以后当用户点击这个书签时,我就会显示注释.以下是我录制的视频介绍: 这个插件将包括以下几个技术点: 添加右键菜单 添加右键菜单.控制右键菜单显示 WindowBeforeRightClick 事件 删除右键菜单 修

Word 2007 转 Word2003格式 docx2doc

啥也不说了,直接上代码. public partial class frmMain : Form { public frmMain() { InitializeComponent(); } private void btnStart_Click(object sender, EventArgs e) { var dilog = new OpenFileDialog(); dilog.RestoreDirectory = true; dilog.AddExtension = true; dilog