Skip to content

Instantly share code, notes, and snippets.

@neerajkumar
Last active May 28, 2018 10:29
Show Gist options
  • Save neerajkumar/bb2679ee14395466b0e94e20cb8c3649 to your computer and use it in GitHub Desktop.
Save neerajkumar/bb2679ee14395466b0e94e20cb8c3649 to your computer and use it in GitHub Desktop.
Hackerrank Test: Counting programmer string in progxrammerrxproxgrammer, xprogxrmaxemrppprmmograeiruu and programmerprogrammer
class String
def maulin! n
slice! n
self
end
def maulin n
dup.maulin! n
end
end
def programmerStrings(s)
programmer = 'programmer'
head = programmer;
index = 0
while(index < s.length) do
pIndex = head.index(s[index])
head = head.maulin(pIndex) unless pIndex.nil?
if head.length == 0
index += 1
break
end
index += 1
end
tail = 'programmer'
j = s.length - 1
while(j >= 0) do
pIndex = tail.index(s[j])
tail = tail.maulin(pIndex) unless pIndex.nil?
if tail.length == 0
j -= 1
break
end
j -= 1
end
# puts head
# puts tail
# puts index
# puts j
return j - index + 1
end
## 'progxrammerrxproxgrammer' => 2
## 'xprogxrmaxemrppprmmograeiruu' => 2
## 'programmerprogrammer' => 0
puts programmerStrings('progxrammerrxproxgrammer')
puts programmerStrings('xprogxrmaxemrppprmmograeiruu')
puts programmerStrings('programmerprogrammer')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment