How to find the max moves using math?












0












$begingroup$


I am designing a game; Here I need to find max score for that I need to find max moves player can make.



for example -




playerA-value = abccd I need to find the substring say bc in playerA-value



Now I deleted it. So playerA-value = acd



algorithm checks for bc and it not found, so max move = 1.




for example -




playerA-value = aeerrb I need to find the substring say er in playerA-value



Now I deleted it. So playerA-value = aerb



algorithm checks for er and it found, above steps repeats, finally max move = 2.




to maximize the score, I can delete it either from the front end of the string or rear end, or combination of both(like first few time front, later check for the back side.



Is there any formula in combination or permutation to find this?










share|cite|improve this question











$endgroup$












  • $begingroup$
    Is the game "Start with a string $s$ and a substring $r$, and try to find the maximum number of consecutive removals of $r$ from $s$"?
    $endgroup$
    – Zubin Mukerjee
    Aug 26 '17 at 3:16










  • $begingroup$
    yea. right, almost same.
    $endgroup$
    – ajayramesh
    Aug 26 '17 at 3:18










  • $begingroup$
    @ZubinMukerjee -please note that, I can find the consecutive removal of r from front side or back side.
    $endgroup$
    – ajayramesh
    Aug 26 '17 at 3:20










  • $begingroup$
    As in, if the substring is er, then both eerr and erer would have max score $2$, right? What about edr? Does edr have score $0$ or $1$?
    $endgroup$
    – Zubin Mukerjee
    Aug 26 '17 at 3:22










  • $begingroup$
    @ZubinMukerjee yes to first question, second question - edr will have 0
    $endgroup$
    – ajayramesh
    Aug 26 '17 at 3:25
















0












$begingroup$


I am designing a game; Here I need to find max score for that I need to find max moves player can make.



for example -




playerA-value = abccd I need to find the substring say bc in playerA-value



Now I deleted it. So playerA-value = acd



algorithm checks for bc and it not found, so max move = 1.




for example -




playerA-value = aeerrb I need to find the substring say er in playerA-value



Now I deleted it. So playerA-value = aerb



algorithm checks for er and it found, above steps repeats, finally max move = 2.




to maximize the score, I can delete it either from the front end of the string or rear end, or combination of both(like first few time front, later check for the back side.



Is there any formula in combination or permutation to find this?










share|cite|improve this question











$endgroup$












  • $begingroup$
    Is the game "Start with a string $s$ and a substring $r$, and try to find the maximum number of consecutive removals of $r$ from $s$"?
    $endgroup$
    – Zubin Mukerjee
    Aug 26 '17 at 3:16










  • $begingroup$
    yea. right, almost same.
    $endgroup$
    – ajayramesh
    Aug 26 '17 at 3:18










  • $begingroup$
    @ZubinMukerjee -please note that, I can find the consecutive removal of r from front side or back side.
    $endgroup$
    – ajayramesh
    Aug 26 '17 at 3:20










  • $begingroup$
    As in, if the substring is er, then both eerr and erer would have max score $2$, right? What about edr? Does edr have score $0$ or $1$?
    $endgroup$
    – Zubin Mukerjee
    Aug 26 '17 at 3:22










  • $begingroup$
    @ZubinMukerjee yes to first question, second question - edr will have 0
    $endgroup$
    – ajayramesh
    Aug 26 '17 at 3:25














0












0








0





$begingroup$


I am designing a game; Here I need to find max score for that I need to find max moves player can make.



for example -




playerA-value = abccd I need to find the substring say bc in playerA-value



Now I deleted it. So playerA-value = acd



algorithm checks for bc and it not found, so max move = 1.




for example -




playerA-value = aeerrb I need to find the substring say er in playerA-value



Now I deleted it. So playerA-value = aerb



algorithm checks for er and it found, above steps repeats, finally max move = 2.




to maximize the score, I can delete it either from the front end of the string or rear end, or combination of both(like first few time front, later check for the back side.



Is there any formula in combination or permutation to find this?










share|cite|improve this question











$endgroup$




I am designing a game; Here I need to find max score for that I need to find max moves player can make.



for example -




playerA-value = abccd I need to find the substring say bc in playerA-value



Now I deleted it. So playerA-value = acd



algorithm checks for bc and it not found, so max move = 1.




for example -




playerA-value = aeerrb I need to find the substring say er in playerA-value



Now I deleted it. So playerA-value = aerb



algorithm checks for er and it found, above steps repeats, finally max move = 2.




to maximize the score, I can delete it either from the front end of the string or rear end, or combination of both(like first few time front, later check for the back side.



Is there any formula in combination or permutation to find this?







combinatorics permutations computer-science






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Aug 26 '17 at 3:17







ajayramesh

















asked Aug 26 '17 at 3:11









ajayrameshajayramesh

1458




1458












  • $begingroup$
    Is the game "Start with a string $s$ and a substring $r$, and try to find the maximum number of consecutive removals of $r$ from $s$"?
    $endgroup$
    – Zubin Mukerjee
    Aug 26 '17 at 3:16










  • $begingroup$
    yea. right, almost same.
    $endgroup$
    – ajayramesh
    Aug 26 '17 at 3:18










  • $begingroup$
    @ZubinMukerjee -please note that, I can find the consecutive removal of r from front side or back side.
    $endgroup$
    – ajayramesh
    Aug 26 '17 at 3:20










  • $begingroup$
    As in, if the substring is er, then both eerr and erer would have max score $2$, right? What about edr? Does edr have score $0$ or $1$?
    $endgroup$
    – Zubin Mukerjee
    Aug 26 '17 at 3:22










  • $begingroup$
    @ZubinMukerjee yes to first question, second question - edr will have 0
    $endgroup$
    – ajayramesh
    Aug 26 '17 at 3:25


















  • $begingroup$
    Is the game "Start with a string $s$ and a substring $r$, and try to find the maximum number of consecutive removals of $r$ from $s$"?
    $endgroup$
    – Zubin Mukerjee
    Aug 26 '17 at 3:16










  • $begingroup$
    yea. right, almost same.
    $endgroup$
    – ajayramesh
    Aug 26 '17 at 3:18










  • $begingroup$
    @ZubinMukerjee -please note that, I can find the consecutive removal of r from front side or back side.
    $endgroup$
    – ajayramesh
    Aug 26 '17 at 3:20










  • $begingroup$
    As in, if the substring is er, then both eerr and erer would have max score $2$, right? What about edr? Does edr have score $0$ or $1$?
    $endgroup$
    – Zubin Mukerjee
    Aug 26 '17 at 3:22










  • $begingroup$
    @ZubinMukerjee yes to first question, second question - edr will have 0
    $endgroup$
    – ajayramesh
    Aug 26 '17 at 3:25
















$begingroup$
Is the game "Start with a string $s$ and a substring $r$, and try to find the maximum number of consecutive removals of $r$ from $s$"?
$endgroup$
– Zubin Mukerjee
Aug 26 '17 at 3:16




$begingroup$
Is the game "Start with a string $s$ and a substring $r$, and try to find the maximum number of consecutive removals of $r$ from $s$"?
$endgroup$
– Zubin Mukerjee
Aug 26 '17 at 3:16












$begingroup$
yea. right, almost same.
$endgroup$
– ajayramesh
Aug 26 '17 at 3:18




$begingroup$
yea. right, almost same.
$endgroup$
– ajayramesh
Aug 26 '17 at 3:18












$begingroup$
@ZubinMukerjee -please note that, I can find the consecutive removal of r from front side or back side.
$endgroup$
– ajayramesh
Aug 26 '17 at 3:20




$begingroup$
@ZubinMukerjee -please note that, I can find the consecutive removal of r from front side or back side.
$endgroup$
– ajayramesh
Aug 26 '17 at 3:20












$begingroup$
As in, if the substring is er, then both eerr and erer would have max score $2$, right? What about edr? Does edr have score $0$ or $1$?
$endgroup$
– Zubin Mukerjee
Aug 26 '17 at 3:22




$begingroup$
As in, if the substring is er, then both eerr and erer would have max score $2$, right? What about edr? Does edr have score $0$ or $1$?
$endgroup$
– Zubin Mukerjee
Aug 26 '17 at 3:22












$begingroup$
@ZubinMukerjee yes to first question, second question - edr will have 0
$endgroup$
– ajayramesh
Aug 26 '17 at 3:25




$begingroup$
@ZubinMukerjee yes to first question, second question - edr will have 0
$endgroup$
– ajayramesh
Aug 26 '17 at 3:25










2 Answers
2






active

oldest

votes


















2












$begingroup$

I wrote the following Java code to output the maximum number of removals of a substring from a string.



The code uses replaceFirst within a while loop, until it can no longer be applied. The code counts the number of times the while loop executes, and this is the integer output.



public class RemoveString {

public static int maxScore(String bigString, String littleString){
String tempString = bigString;
int runningCount = 0;
while(!(tempString.replaceFirst(littleString, "").equals(tempString))){
tempString = tempString.replaceFirst(littleString, "");
runningCount++;
}
return runningCount;
}

public static void main(String args){
System.out.println(maxScore("ababa", "er")); // Should be 0
System.out.println(maxScore("abaer", "er")); // Should be 1
System.out.println(maxScore("erababa", "er")); // Should be 1
System.out.println(maxScore("zzzeraba", "er")); // Should be 1
System.out.println(maxScore("aerbabaer", "er")); // Should be 2
System.out.println(maxScore("aeerra", "er")); // Should be 2
System.out.println(maxScore("aerera", "er")); // Should be 2
System.out.println(maxScore("aerbera", "er")); //Should be 2
System.out.println(maxScore("aberebraba", "er")); // Should be 1

}
}


I ran the test code, and all nine test cases gave the expected output.






share|cite|improve this answer









$endgroup$









  • 1




    $begingroup$
    this will not work for this case- System.out.println(maxScore("ababaa", "aba")); it should give 2, but your code will print 1.
    $endgroup$
    – dfg
    Jun 3 '18 at 3:55










  • $begingroup$
    You're right! Replacing the first appearance is not always ideal. I think that fact increases the time complexity of the problem. I will try to find a method that actually works when replacing the first appearance does not give the optimal solution.
    $endgroup$
    – Zubin Mukerjee
    Jun 3 '18 at 14:49



















1












$begingroup$

This can be easily solved using BFS. The following code gives the correct output for all the inputs mentioned above. I've used JUnit.Assert.assertEquals().



import java.util.Deque;
import java.util.LinkedList;
import static org.junit.Assert.assertEquals;

public class RemoveString {
static class Moves {
String str ;
int moves ;
public Moves(String str, int moves) {
this.str = str ;
this.moves = moves ;
}
}

static
public int maxMoves(String s, String t) {
int max = 0 ;
if (s == null || t == null || t.length() > s.length())
return max ;
Deque<Moves> q = new LinkedList<>() ;
q.offer(new Moves(s, 0)) ;
while (!q.isEmpty()) {
for (int sz = q.size() ; sz > 0 ; -- sz) {
Moves curMove = q.poll() ;
max = Math.max(max, curMove.moves) ;
if (curMove.str.contains(t)) {
int nextStart = curMove.str.indexOf(t, 0) ;
if (nextStart != -1) {
q.offer(new Moves(curMove.str.substring(0, nextStart) + curMove.str.substring(nextStart+t.length()), curMove.moves+1)) ;
}
int lastStart = curMove.str.lastIndexOf(t, curMove.str.length()) ;
if (lastStart != -1) {
q.offer(new Moves(curMove.str.substring(0, lastStart) + curMove.str.substring(lastStart+t.length()), curMove.moves+1)) ;
}
}
}
}
return max ;
}

public static void main(String args) {
assertEquals(3, maxMoves("babba", "b"));
assertEquals(2, maxMoves("aabb", "ab"));
assertEquals(2, maxMoves("ababaa", "aba"));
assertEquals(1, maxMoves("abaer", "er")); // Should be 1
assertEquals(1, maxMoves("erababa", "er")); // Should be 1
assertEquals(1, maxMoves("zzzeraba", "er")); // Should be 1
assertEquals(2, maxMoves("aerbabaer", "er")); // Should be 2
assertEquals(2, maxMoves("aeerra", "er")); // Should be 2
assertEquals(2, maxMoves("aerera", "er")); // Should be 2
assertEquals(2, maxMoves("aerbera", "er")); //Should be 2
assertEquals(1, maxMoves("aberebraba", "er")); // Should be 1
}
}





share|cite|improve this answer









$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "69"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    noCode: true, onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2406384%2fhow-to-find-the-max-moves-using-math%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2












    $begingroup$

    I wrote the following Java code to output the maximum number of removals of a substring from a string.



    The code uses replaceFirst within a while loop, until it can no longer be applied. The code counts the number of times the while loop executes, and this is the integer output.



    public class RemoveString {

    public static int maxScore(String bigString, String littleString){
    String tempString = bigString;
    int runningCount = 0;
    while(!(tempString.replaceFirst(littleString, "").equals(tempString))){
    tempString = tempString.replaceFirst(littleString, "");
    runningCount++;
    }
    return runningCount;
    }

    public static void main(String args){
    System.out.println(maxScore("ababa", "er")); // Should be 0
    System.out.println(maxScore("abaer", "er")); // Should be 1
    System.out.println(maxScore("erababa", "er")); // Should be 1
    System.out.println(maxScore("zzzeraba", "er")); // Should be 1
    System.out.println(maxScore("aerbabaer", "er")); // Should be 2
    System.out.println(maxScore("aeerra", "er")); // Should be 2
    System.out.println(maxScore("aerera", "er")); // Should be 2
    System.out.println(maxScore("aerbera", "er")); //Should be 2
    System.out.println(maxScore("aberebraba", "er")); // Should be 1

    }
    }


    I ran the test code, and all nine test cases gave the expected output.






    share|cite|improve this answer









    $endgroup$









    • 1




      $begingroup$
      this will not work for this case- System.out.println(maxScore("ababaa", "aba")); it should give 2, but your code will print 1.
      $endgroup$
      – dfg
      Jun 3 '18 at 3:55










    • $begingroup$
      You're right! Replacing the first appearance is not always ideal. I think that fact increases the time complexity of the problem. I will try to find a method that actually works when replacing the first appearance does not give the optimal solution.
      $endgroup$
      – Zubin Mukerjee
      Jun 3 '18 at 14:49
















    2












    $begingroup$

    I wrote the following Java code to output the maximum number of removals of a substring from a string.



    The code uses replaceFirst within a while loop, until it can no longer be applied. The code counts the number of times the while loop executes, and this is the integer output.



    public class RemoveString {

    public static int maxScore(String bigString, String littleString){
    String tempString = bigString;
    int runningCount = 0;
    while(!(tempString.replaceFirst(littleString, "").equals(tempString))){
    tempString = tempString.replaceFirst(littleString, "");
    runningCount++;
    }
    return runningCount;
    }

    public static void main(String args){
    System.out.println(maxScore("ababa", "er")); // Should be 0
    System.out.println(maxScore("abaer", "er")); // Should be 1
    System.out.println(maxScore("erababa", "er")); // Should be 1
    System.out.println(maxScore("zzzeraba", "er")); // Should be 1
    System.out.println(maxScore("aerbabaer", "er")); // Should be 2
    System.out.println(maxScore("aeerra", "er")); // Should be 2
    System.out.println(maxScore("aerera", "er")); // Should be 2
    System.out.println(maxScore("aerbera", "er")); //Should be 2
    System.out.println(maxScore("aberebraba", "er")); // Should be 1

    }
    }


    I ran the test code, and all nine test cases gave the expected output.






    share|cite|improve this answer









    $endgroup$









    • 1




      $begingroup$
      this will not work for this case- System.out.println(maxScore("ababaa", "aba")); it should give 2, but your code will print 1.
      $endgroup$
      – dfg
      Jun 3 '18 at 3:55










    • $begingroup$
      You're right! Replacing the first appearance is not always ideal. I think that fact increases the time complexity of the problem. I will try to find a method that actually works when replacing the first appearance does not give the optimal solution.
      $endgroup$
      – Zubin Mukerjee
      Jun 3 '18 at 14:49














    2












    2








    2





    $begingroup$

    I wrote the following Java code to output the maximum number of removals of a substring from a string.



    The code uses replaceFirst within a while loop, until it can no longer be applied. The code counts the number of times the while loop executes, and this is the integer output.



    public class RemoveString {

    public static int maxScore(String bigString, String littleString){
    String tempString = bigString;
    int runningCount = 0;
    while(!(tempString.replaceFirst(littleString, "").equals(tempString))){
    tempString = tempString.replaceFirst(littleString, "");
    runningCount++;
    }
    return runningCount;
    }

    public static void main(String args){
    System.out.println(maxScore("ababa", "er")); // Should be 0
    System.out.println(maxScore("abaer", "er")); // Should be 1
    System.out.println(maxScore("erababa", "er")); // Should be 1
    System.out.println(maxScore("zzzeraba", "er")); // Should be 1
    System.out.println(maxScore("aerbabaer", "er")); // Should be 2
    System.out.println(maxScore("aeerra", "er")); // Should be 2
    System.out.println(maxScore("aerera", "er")); // Should be 2
    System.out.println(maxScore("aerbera", "er")); //Should be 2
    System.out.println(maxScore("aberebraba", "er")); // Should be 1

    }
    }


    I ran the test code, and all nine test cases gave the expected output.






    share|cite|improve this answer









    $endgroup$



    I wrote the following Java code to output the maximum number of removals of a substring from a string.



    The code uses replaceFirst within a while loop, until it can no longer be applied. The code counts the number of times the while loop executes, and this is the integer output.



    public class RemoveString {

    public static int maxScore(String bigString, String littleString){
    String tempString = bigString;
    int runningCount = 0;
    while(!(tempString.replaceFirst(littleString, "").equals(tempString))){
    tempString = tempString.replaceFirst(littleString, "");
    runningCount++;
    }
    return runningCount;
    }

    public static void main(String args){
    System.out.println(maxScore("ababa", "er")); // Should be 0
    System.out.println(maxScore("abaer", "er")); // Should be 1
    System.out.println(maxScore("erababa", "er")); // Should be 1
    System.out.println(maxScore("zzzeraba", "er")); // Should be 1
    System.out.println(maxScore("aerbabaer", "er")); // Should be 2
    System.out.println(maxScore("aeerra", "er")); // Should be 2
    System.out.println(maxScore("aerera", "er")); // Should be 2
    System.out.println(maxScore("aerbera", "er")); //Should be 2
    System.out.println(maxScore("aberebraba", "er")); // Should be 1

    }
    }


    I ran the test code, and all nine test cases gave the expected output.







    share|cite|improve this answer












    share|cite|improve this answer



    share|cite|improve this answer










    answered Aug 26 '17 at 3:49









    Zubin MukerjeeZubin Mukerjee

    15.2k32658




    15.2k32658








    • 1




      $begingroup$
      this will not work for this case- System.out.println(maxScore("ababaa", "aba")); it should give 2, but your code will print 1.
      $endgroup$
      – dfg
      Jun 3 '18 at 3:55










    • $begingroup$
      You're right! Replacing the first appearance is not always ideal. I think that fact increases the time complexity of the problem. I will try to find a method that actually works when replacing the first appearance does not give the optimal solution.
      $endgroup$
      – Zubin Mukerjee
      Jun 3 '18 at 14:49














    • 1




      $begingroup$
      this will not work for this case- System.out.println(maxScore("ababaa", "aba")); it should give 2, but your code will print 1.
      $endgroup$
      – dfg
      Jun 3 '18 at 3:55










    • $begingroup$
      You're right! Replacing the first appearance is not always ideal. I think that fact increases the time complexity of the problem. I will try to find a method that actually works when replacing the first appearance does not give the optimal solution.
      $endgroup$
      – Zubin Mukerjee
      Jun 3 '18 at 14:49








    1




    1




    $begingroup$
    this will not work for this case- System.out.println(maxScore("ababaa", "aba")); it should give 2, but your code will print 1.
    $endgroup$
    – dfg
    Jun 3 '18 at 3:55




    $begingroup$
    this will not work for this case- System.out.println(maxScore("ababaa", "aba")); it should give 2, but your code will print 1.
    $endgroup$
    – dfg
    Jun 3 '18 at 3:55












    $begingroup$
    You're right! Replacing the first appearance is not always ideal. I think that fact increases the time complexity of the problem. I will try to find a method that actually works when replacing the first appearance does not give the optimal solution.
    $endgroup$
    – Zubin Mukerjee
    Jun 3 '18 at 14:49




    $begingroup$
    You're right! Replacing the first appearance is not always ideal. I think that fact increases the time complexity of the problem. I will try to find a method that actually works when replacing the first appearance does not give the optimal solution.
    $endgroup$
    – Zubin Mukerjee
    Jun 3 '18 at 14:49











    1












    $begingroup$

    This can be easily solved using BFS. The following code gives the correct output for all the inputs mentioned above. I've used JUnit.Assert.assertEquals().



    import java.util.Deque;
    import java.util.LinkedList;
    import static org.junit.Assert.assertEquals;

    public class RemoveString {
    static class Moves {
    String str ;
    int moves ;
    public Moves(String str, int moves) {
    this.str = str ;
    this.moves = moves ;
    }
    }

    static
    public int maxMoves(String s, String t) {
    int max = 0 ;
    if (s == null || t == null || t.length() > s.length())
    return max ;
    Deque<Moves> q = new LinkedList<>() ;
    q.offer(new Moves(s, 0)) ;
    while (!q.isEmpty()) {
    for (int sz = q.size() ; sz > 0 ; -- sz) {
    Moves curMove = q.poll() ;
    max = Math.max(max, curMove.moves) ;
    if (curMove.str.contains(t)) {
    int nextStart = curMove.str.indexOf(t, 0) ;
    if (nextStart != -1) {
    q.offer(new Moves(curMove.str.substring(0, nextStart) + curMove.str.substring(nextStart+t.length()), curMove.moves+1)) ;
    }
    int lastStart = curMove.str.lastIndexOf(t, curMove.str.length()) ;
    if (lastStart != -1) {
    q.offer(new Moves(curMove.str.substring(0, lastStart) + curMove.str.substring(lastStart+t.length()), curMove.moves+1)) ;
    }
    }
    }
    }
    return max ;
    }

    public static void main(String args) {
    assertEquals(3, maxMoves("babba", "b"));
    assertEquals(2, maxMoves("aabb", "ab"));
    assertEquals(2, maxMoves("ababaa", "aba"));
    assertEquals(1, maxMoves("abaer", "er")); // Should be 1
    assertEquals(1, maxMoves("erababa", "er")); // Should be 1
    assertEquals(1, maxMoves("zzzeraba", "er")); // Should be 1
    assertEquals(2, maxMoves("aerbabaer", "er")); // Should be 2
    assertEquals(2, maxMoves("aeerra", "er")); // Should be 2
    assertEquals(2, maxMoves("aerera", "er")); // Should be 2
    assertEquals(2, maxMoves("aerbera", "er")); //Should be 2
    assertEquals(1, maxMoves("aberebraba", "er")); // Should be 1
    }
    }





    share|cite|improve this answer









    $endgroup$


















      1












      $begingroup$

      This can be easily solved using BFS. The following code gives the correct output for all the inputs mentioned above. I've used JUnit.Assert.assertEquals().



      import java.util.Deque;
      import java.util.LinkedList;
      import static org.junit.Assert.assertEquals;

      public class RemoveString {
      static class Moves {
      String str ;
      int moves ;
      public Moves(String str, int moves) {
      this.str = str ;
      this.moves = moves ;
      }
      }

      static
      public int maxMoves(String s, String t) {
      int max = 0 ;
      if (s == null || t == null || t.length() > s.length())
      return max ;
      Deque<Moves> q = new LinkedList<>() ;
      q.offer(new Moves(s, 0)) ;
      while (!q.isEmpty()) {
      for (int sz = q.size() ; sz > 0 ; -- sz) {
      Moves curMove = q.poll() ;
      max = Math.max(max, curMove.moves) ;
      if (curMove.str.contains(t)) {
      int nextStart = curMove.str.indexOf(t, 0) ;
      if (nextStart != -1) {
      q.offer(new Moves(curMove.str.substring(0, nextStart) + curMove.str.substring(nextStart+t.length()), curMove.moves+1)) ;
      }
      int lastStart = curMove.str.lastIndexOf(t, curMove.str.length()) ;
      if (lastStart != -1) {
      q.offer(new Moves(curMove.str.substring(0, lastStart) + curMove.str.substring(lastStart+t.length()), curMove.moves+1)) ;
      }
      }
      }
      }
      return max ;
      }

      public static void main(String args) {
      assertEquals(3, maxMoves("babba", "b"));
      assertEquals(2, maxMoves("aabb", "ab"));
      assertEquals(2, maxMoves("ababaa", "aba"));
      assertEquals(1, maxMoves("abaer", "er")); // Should be 1
      assertEquals(1, maxMoves("erababa", "er")); // Should be 1
      assertEquals(1, maxMoves("zzzeraba", "er")); // Should be 1
      assertEquals(2, maxMoves("aerbabaer", "er")); // Should be 2
      assertEquals(2, maxMoves("aeerra", "er")); // Should be 2
      assertEquals(2, maxMoves("aerera", "er")); // Should be 2
      assertEquals(2, maxMoves("aerbera", "er")); //Should be 2
      assertEquals(1, maxMoves("aberebraba", "er")); // Should be 1
      }
      }





      share|cite|improve this answer









      $endgroup$
















        1












        1








        1





        $begingroup$

        This can be easily solved using BFS. The following code gives the correct output for all the inputs mentioned above. I've used JUnit.Assert.assertEquals().



        import java.util.Deque;
        import java.util.LinkedList;
        import static org.junit.Assert.assertEquals;

        public class RemoveString {
        static class Moves {
        String str ;
        int moves ;
        public Moves(String str, int moves) {
        this.str = str ;
        this.moves = moves ;
        }
        }

        static
        public int maxMoves(String s, String t) {
        int max = 0 ;
        if (s == null || t == null || t.length() > s.length())
        return max ;
        Deque<Moves> q = new LinkedList<>() ;
        q.offer(new Moves(s, 0)) ;
        while (!q.isEmpty()) {
        for (int sz = q.size() ; sz > 0 ; -- sz) {
        Moves curMove = q.poll() ;
        max = Math.max(max, curMove.moves) ;
        if (curMove.str.contains(t)) {
        int nextStart = curMove.str.indexOf(t, 0) ;
        if (nextStart != -1) {
        q.offer(new Moves(curMove.str.substring(0, nextStart) + curMove.str.substring(nextStart+t.length()), curMove.moves+1)) ;
        }
        int lastStart = curMove.str.lastIndexOf(t, curMove.str.length()) ;
        if (lastStart != -1) {
        q.offer(new Moves(curMove.str.substring(0, lastStart) + curMove.str.substring(lastStart+t.length()), curMove.moves+1)) ;
        }
        }
        }
        }
        return max ;
        }

        public static void main(String args) {
        assertEquals(3, maxMoves("babba", "b"));
        assertEquals(2, maxMoves("aabb", "ab"));
        assertEquals(2, maxMoves("ababaa", "aba"));
        assertEquals(1, maxMoves("abaer", "er")); // Should be 1
        assertEquals(1, maxMoves("erababa", "er")); // Should be 1
        assertEquals(1, maxMoves("zzzeraba", "er")); // Should be 1
        assertEquals(2, maxMoves("aerbabaer", "er")); // Should be 2
        assertEquals(2, maxMoves("aeerra", "er")); // Should be 2
        assertEquals(2, maxMoves("aerera", "er")); // Should be 2
        assertEquals(2, maxMoves("aerbera", "er")); //Should be 2
        assertEquals(1, maxMoves("aberebraba", "er")); // Should be 1
        }
        }





        share|cite|improve this answer









        $endgroup$



        This can be easily solved using BFS. The following code gives the correct output for all the inputs mentioned above. I've used JUnit.Assert.assertEquals().



        import java.util.Deque;
        import java.util.LinkedList;
        import static org.junit.Assert.assertEquals;

        public class RemoveString {
        static class Moves {
        String str ;
        int moves ;
        public Moves(String str, int moves) {
        this.str = str ;
        this.moves = moves ;
        }
        }

        static
        public int maxMoves(String s, String t) {
        int max = 0 ;
        if (s == null || t == null || t.length() > s.length())
        return max ;
        Deque<Moves> q = new LinkedList<>() ;
        q.offer(new Moves(s, 0)) ;
        while (!q.isEmpty()) {
        for (int sz = q.size() ; sz > 0 ; -- sz) {
        Moves curMove = q.poll() ;
        max = Math.max(max, curMove.moves) ;
        if (curMove.str.contains(t)) {
        int nextStart = curMove.str.indexOf(t, 0) ;
        if (nextStart != -1) {
        q.offer(new Moves(curMove.str.substring(0, nextStart) + curMove.str.substring(nextStart+t.length()), curMove.moves+1)) ;
        }
        int lastStart = curMove.str.lastIndexOf(t, curMove.str.length()) ;
        if (lastStart != -1) {
        q.offer(new Moves(curMove.str.substring(0, lastStart) + curMove.str.substring(lastStart+t.length()), curMove.moves+1)) ;
        }
        }
        }
        }
        return max ;
        }

        public static void main(String args) {
        assertEquals(3, maxMoves("babba", "b"));
        assertEquals(2, maxMoves("aabb", "ab"));
        assertEquals(2, maxMoves("ababaa", "aba"));
        assertEquals(1, maxMoves("abaer", "er")); // Should be 1
        assertEquals(1, maxMoves("erababa", "er")); // Should be 1
        assertEquals(1, maxMoves("zzzeraba", "er")); // Should be 1
        assertEquals(2, maxMoves("aerbabaer", "er")); // Should be 2
        assertEquals(2, maxMoves("aeerra", "er")); // Should be 2
        assertEquals(2, maxMoves("aerera", "er")); // Should be 2
        assertEquals(2, maxMoves("aerbera", "er")); //Should be 2
        assertEquals(1, maxMoves("aberebraba", "er")); // Should be 1
        }
        }






        share|cite|improve this answer












        share|cite|improve this answer



        share|cite|improve this answer










        answered Dec 24 '18 at 6:39









        Raghu KumarRaghu Kumar

        111




        111






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Mathematics Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2406384%2fhow-to-find-the-max-moves-using-math%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen