Skip to content

Instantly share code, notes, and snippets.

@neerajkumar
Created May 28, 2018 10:32
Show Gist options
  • Save neerajkumar/58d7a8c48dfb2675c2c444524f511a0a to your computer and use it in GitHub Desktop.
Save neerajkumar/58d7a8c48dfb2675c2c444524f511a0a to your computer and use it in GitHub Desktop.
Hackerrank Test: Counting programmer string in progxrammerrxproxgrammer, xprogxrmaxemrppprmmograeiruu and programmerprogrammer
public class Solution4 {
static int solve(String str) {
String programmer = "programmer";
String head = programmer;
int i = 0;
// programmer
for (; i < str.length(); i++) {
int pIndex = head.indexOf(str.charAt(i));
if (pIndex != -1) {
head = head.substring(0, pIndex).concat(head.substring(pIndex + 1));
System.out.println(head);
}
if (head.length() == 0) {
i++;
break;
}
}
String tail = programmer;
int j = str.length() - 1;
// programmer
for (; j >= 0; j--) {
int pIndex = tail.indexOf(str.charAt(j));
if (pIndex != -1) {
tail = tail.substring(0, pIndex).concat(tail.substring(pIndex + 1));
System.out.println(tail);
}
if (tail.length() == 0) {
j--;
break;
}
}
// System.out.println(head);
// System.out.println(tail);
// System.out.println(i);
// System.out.println(j);
return j - i + 1;
}
public static void main(String[] args) {
System.out.println(solve("progxrammerrxproxgrammer"));
// System.out.println(solve("xprogxrmaxemrppprmmograeiruu"));
// System.out.println(solve("programmerprogrammer"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment