Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- indexOf = (a, b)->
- candidates = []
- seen = {}
- furthestVisited = -1
- seqStart = b[0]
- check = (idx)->
- valid = true
- for j from 1 to Math.floor b.length/2
- idxLeft = idx + j
- idxRight = b.length - j + i
- if idxRight >= a.length
- return -1
- left = a[idxLeft]
- right = a[idxRight]
- if !(left==b[j] and right==b[b.length - j])
- valid = false
- if idxLeft==idxRight
- if left == seqStart and !seen[idxLeft]
- seen[idxLeft] = true
- candidates.push idxLeft
- else
- if left == seqStart and !seen[idxLeft]
- seen[idxLeft] = true
- candidates.push idxLeft
- if right == seqStart and !seen[idxRight]
- seen[idxRight] = true
- candidates.push idxRight
- if valid
- return idx
- furthestVisited := Math.max(idx, b.length - 1)
- return -1
- for i from 0 to a.length
- furthestVisited = Math.max(i, furthestVisited)
- if a[i] == seqStart
- possible = check i
- if possible == null
- return -1
- if possible > -1
- return possible
- while candidates.length!=0
- # console.log candidates
- currCandidate = candidates.shift!
- possible = check currCandidate
- if possible == null
- return -1
- if possible > -1
- return possible
- i = furthestVisited
- return -1
- console.log indexOf('aaabaaaaaaaaaaab', 'aaaab')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement