File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ Pattern matching is case insensitive as
3+ the inputs are converted to lower case before the
4+ algorithm is run.
5+
6+ The algorithm will run through the entire text and
7+ return the starting index if the given pattern is
8+ available in the text
9+ */
10+ const checkIfPatternExists = ( text , pattern ) => {
11+ const textLength = text . length // Store the length of the text in a variable
12+ const patternLength = pattern . length // Store the length of the pattern in a variable
13+
14+ // Iterate through the text until the textlength - patternlength index
15+ for ( let i = 0 ; i <= textLength - patternLength ; i ++ ) {
16+ // For each character in the text check if the subsequent character
17+ // are matching the given pattern; if not break from the condition
18+ for ( let j = 0 ; j < textLength ; j ++ ) {
19+ if ( text [ i + j ] !== pattern [ j ] ) break
20+
21+ // For each iteration of j check if the value of
22+ // j + 1 is equal to the length of the pattern
23+ if ( j + 1 === patternLength ) {
24+ console . log ( `Given pattern is found at index ${ i } ` )
25+ }
26+ }
27+ }
28+ }
29+
30+ const main = ( ) => {
31+ const text = 'AABAACAADAABAAAABAA'
32+ const pattern = 'AABA'
33+ checkIfPatternExists ( text . toLowerCase ( ) , pattern . toLowerCase ( ) )
34+ }
35+
36+ main ( )
You can’t perform that action at this time.
0 commit comments