Solved

# javascript optimization on finding substrings

Posted on 2015-02-10
70 Views
Hi Experts,

I have one string S that can be say, 200 characters long.   I have 10000 other strings that I want to see if they are in S.

My solution needs to work on IE9, IE10, IE11, Firefox and chrome.  I would preferably not like to use one method on one browser and another method for special case browsers that don't support functions.

What can I use to optimize this?

Thanks a lot!
Mike
0
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 9

Accepted Solution

James Bilous earned 250 total points
ID: 40600938
The Knuth-Morris-Pratt algorithm is pretty quick and is O(S). That's about as good as you're going to get without getting overly complicated. You'll probably need to implement it in JS yourself.

``````algorithm kmp_search:
input:
an array of characters, S (the text to be searched)
an array of characters, W (the word sought)
output:
an integer (the zero-based position in S at which W is found)

define variables:
an integer, m ← 0 (the beginning of the current match in S)
an integer, i ← 0 (the position of the current character in W)
an array of integers, T (the table, computed elsewhere)

while m + i < length(S) do
if W[i] = S[m + i] then
if i = length(W) - 1 then
return m
let i ← i + 1
else
if T[i] > -1 then
let m ← m + i - T[i], i ← T[i]
else
let i ← 0, m ← m + 1

(if we reach here, we have searched all of S unsuccessfully)
return the length of S
``````
0

LVL 37

Expert Comment

ID: 40600939
FYI: You'd better not tagged this question as a Java Programming language question.
Java and JavaScript are two different languages.
0

LVL 34

Assisted Solution

Slick812 earned 250 total points
ID: 40601589
greetings thready, , Not sure about exact nature of what you describe as "to see if they are in S", If all you need is a return of "TRUE" if array value IS in string, Then I believe that the -
for (s=0; s < seek.length; ++s)
if (str.indexOf(seek[s]) > -1) found[s] = true; else found[s] = false;

would be the most effective.
I found this in the AJAX section, not sure I see any reference to a server side response here? ?
0

LVL 1

Author Closing Comment

ID: 40609265
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
###### Suggested Courses
Course of the Month11 days, 1 hour left to enroll