Problem
Given two strings, you have to find the missing string.
ExampleGiven a string str1 = This is an example
Given another string str2 = is example
Return ["This", "an"]
Solutionpublic class Solution { /* * @param : a given string * @param : another given string * @return: An array of missing string */ public ListmissingString(String str1, String str2) { // Write your code here List res = new ArrayList<>(); String[] s1 = str1.split(" "); String[] s2 = str2.split(" "); if (s1.length == s2.length) { return res; } //assume s1.length > s2.length if (s1.length < s2.length) { String[] temp = s1; s1 = s2; s2 = temp; } Set unique = new HashSet<>(); //save the short string array in hashset for (String s: s2) { unique.add(s); } //check the long string array and put the missing strings in result for (String s: s1) { if (!unique.contains(s)) { res.add(s); } } return res; } };
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/70943.html
摘要:找第一个缺失的正整数,只要先按顺序排列好,也就是,找到第一个和不对应的数就可以了。注意数组的从开始,而正整数从开始,所以重写排列的时候要注意换成,而就是从开始的数组中的元素。 Problem Given an unsorted integer array, find the first missing positive integer. Example Given [1,2,0] re...
摘要:求和相减是先求出到这个等差数列的和,再减去实际数组的和,就是缺失的数,第二种方法是,只要先按顺序排列好,用二分法找到第一个和不相等的数就可以了。二分法求和相减法共个数,多加了一个异或法 Problem Given an array contains N numbers of 0 .. N, find which number doesnt exist in the array. Exa...
Problem The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetit...
LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...
Problem Given a string s and a dictionary of words dict, determine if s can be break into a space-separated sequence of one or more dictionary words. Example Given s = lintcode, dict = [lint, code]. R...
阅读 1028·2023-04-26 02:02
阅读 2371·2021-09-26 10:11
阅读 3522·2019-08-30 13:10
阅读 3708·2019-08-29 17:12
阅读 688·2019-08-29 14:20
阅读 2169·2019-08-28 18:19
阅读 2173·2019-08-26 13:52
阅读 934·2019-08-26 13:43