javascript实现:在N个字符串中找出最长的公子串
'use strict'
module.exports = function 找出最长公子串 (...strings) {
let setsOfSubstrings = []
strings.reduce((accumulator, currentValue) => {
setsOfSubstrings.push(findAllSubStringsFromTwoString(accumulator, currentValue))
return accumulator
})
return findTheLongest(findIntersection(...setsOfSubstrings))
}
function findIntersection (...sets) {
/**
*
*
* @param {sets} a list of sets
* @returns {array} the elements of the intersection of given sets.
*/
return sets.reduce((accumulator, currentValue) => [...accumulator].filter(value => currentValue.has(value)))
}
function findAllSubStringsFromTwoString (string1, string2) {
let allSubstrings = new Set()
for (let index = 0; index < string1.length; index++) {
let length = 1, substring = string1.substr(index, length)
while (string2.indexOf(substring) !== -1 && length <= string1.length) { // 当string2内含有substring时
allSubstrings.add(substring)
substring = string1.substr(index, length++)
}
}
return allSubstrings
}
function findTheLongest (strings) {
return strings.reduce((accumulator, currentValue) => findTheLonger(accumulator, currentValue), '')
}
function findTheLonger (string1, string2) {
return (string1.length > string2.length) ? (string1) : (string2)
}
之前我给出的程序代码是错误的,没想到居然还被一些博客转载了。。
javascript实现:在N个字符串中找出最长的公子串的更多相关文章
- 最长(大)回文串的查找(字符串中找出最长的回文串)PHP实现
首先还是先解释一下什么是回文串:就是从左到右或者从右到左读,都是同样的字符串.比如:上海自来水来自海上,bob等等. 那么什么又是找出最长回文串呢? 例如:字符串abcdefedcfggggggfc, ...
- C语言:从p所指字符串中找出ASCII码最大的字符,将其放在第一个位置上,并将该字符前的原字符向后顺序移动。-使字符串的前导*号不得多于n个,若多余n个,则删除多余的*号,
//fun函数:从p所指字符串中找出ASCII码最大的字符,将其放在第一个位置上,并将该字符前的原字符向后顺序移动. #include <stdio.h> void fun( char * ...
- hdu 1595 find the longest of the shortest【最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条】
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- 输入n个字符串,找出最长最短字符串(若有个数相同的,都打印出来)
首先,要求找到最长最短字符串,我们应该用数组将其存起来,输入的个数是不固定的,我们就可以用Scanner获取要输入的个数,最终找到的个数也不固定,我们可以封装两个方法,并且返回值类型为数组. 我遇到的 ...
- C#中的字符串处理——找出最长数字子串
百度测试部2015年10月份的面试题之——字符串处理,找出最长的子串. 代码如下: private static string SelectNumberFromString(string input) ...
- 在字符串中找出第一个只出现一次的字符,Python实现
要求: 1. 不能依赖库函数直接实现此功能,需使用基础的数据结构实现 2. 时间复杂度 O(n) 思路: 1. 用字典存储每个字符在字符串中出现的次数 2. 列表是有序的,用来存储字符的出现先后 3. ...
- leetcode 5 :Longest Palindromic Substring 找出最长回文子串
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- FCC JS基础算法题(3):Find the Longest Word in a String (找出最长单词)
题目描述: 在句子中找出最长的单词,并返回它的长度.函数的返回值应该是一个数字. 基本思路,将字符串转换成数组,然后得出数组中单个元素的长度,对长度进行排序,返回最大的一个 代码: function ...
- 海量数据处理 - 10亿个数中找出最大的10000个数(top K问题)
前两天面试3面学长问我的这个问题(想说TEG的3个面试学长都是好和蔼,希望能完成最后一面,各方面原因造成我无比想去鹅场的心已经按捺不住了),这个问题还是建立最小堆比较好一些. 先拿10000个数建堆, ...
随机推荐
- h5 播放器 -2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 虚拟化(四):vsphere高可用功能前提-共享存储搭建
虚拟化(一):虚拟化及vmware产品介绍 虚拟化(二):虚拟化及vmware workstation产品使用 虚拟化(三):vsphere套件的安装注意及使用 虚拟化(四):vsphere高可用功能 ...
- cocos2d-x andriod, Box2D.h: No such file or directory
原文链接:r=47980">http://www.cocos2d-x.org/forums/6/topics/47503? r=47980 You need to include li ...
- Implementing Software Timers - Don Libes
在看APUE习题10.5的时候提示了这篇文章,讲的非常清晰,设计也非常巧妙,所以把原文放在这里.值得自己去实现. Title: Implementing Software Timers By ...
- hdu 1050 Moving Tables(迷之贪心...)
题意:有400间房间按题目中图片所给的方式排列,然后给出要移动的n张桌子所要移动的范围,每张桌子要移动的范围不能出现重叠的区域:问最少要多少次才能移动完所有的桌子. 题解思路:把题目转换下,就是有n个 ...
- HDOJ 2196 Computer 树的直径
由树的直径定义可得,树上随意一点到树的直径上的两个端点之中的一个的距离是最长的... 三遍BFS求树的直径并预处理距离....... Computer Time Limit: 1000/1000 MS ...
- Qt graphic item日记
今天在用用graphic view 加入graphic item的时候要引入一个context menu,自然就要对context menu上的action进行slot处理.可是graphic ite ...
- storm 并行度
1个worker进程运行的是1个topology的子集(注:不会出现1个worker为多个topology服务).1个worker进程会启动1个或多个executor线程来运行1个topology的c ...
- tensorflow在windows操作系统上的安装
在电脑上安装PyCharm和Python3,然后把Python3的安装路径写进系统变量里,Python安装完之后, https://bootstrap.pypa.io/get-pip.py,把这页的代 ...
- Rails5 关联表格搜索
创建: 2017/08/13 other_type_car = Car.joins(:car_type).active.find_by(car_type: car_type) @recomme ...