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; } };
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/70943.html
摘要:找第一個(gè)缺失的正整數(shù),只要先按順序排列好,也就是,找到第一個(gè)和不對(duì)應(yīng)的數(shù)就可以了。注意數(shù)組的從開始,而正整數(shù)從開始,所以重寫排列的時(shí)候要注意換成,而就是從開始的數(shù)組中的元素。 Problem Given an unsorted integer array, find the first missing positive integer. Example Given [1,2,0] re...
摘要:求和相減是先求出到這個(gè)等差數(shù)列的和,再減去實(shí)際數(shù)組的和,就是缺失的數(shù),第二種方法是,只要先按順序排列好,用二分法找到第一個(gè)和不相等的數(shù)就可以了。二分法求和相減法共個(gè)數(shù),多加了一個(gè)異或法 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...
閱讀 1072·2023-04-26 02:02
閱讀 2413·2021-09-26 10:11
閱讀 3567·2019-08-30 13:10
閱讀 3756·2019-08-29 17:12
閱讀 729·2019-08-29 14:20
閱讀 2196·2019-08-28 18:19
閱讀 2245·2019-08-26 13:52
閱讀 968·2019-08-26 13:43