Created
May 28, 2018 10:32
-
-
Save neerajkumar/58d7a8c48dfb2675c2c444524f511a0a to your computer and use it in GitHub Desktop.
Hackerrank Test: Counting programmer string in progxrammerrxproxgrammer, xprogxrmaxemrppprmmograeiruu and programmerprogrammer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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