Given two integers A and B return string containing letters “a” and “b” with no three consecutive...












-2














Asked in coding challenge.



Example:



A = 5 and B = 3 return "aabaabab" or "abbaabaa" both are correct answers
A = 1 and B = 4 return "bbabb"
constraints : ignore cases where A = 0 and B >= 3 vice-versa









share|improve this question






















  • Seems there is something missing in the question, or is this it?
    – Surt
    Nov 13 at 6:20






  • 1




    So, what is your problem?
    – Andreas
    Nov 13 at 6:28










  • This is it. We need to write a function that will return string like above
    – Shailesh Shekhawat
    Nov 13 at 6:30






  • 1




    Compare a and b values. Let's say if a is larger, then create a string with only a's and then insert b after every 2 a's. If there are still b's left, then insert b after b that was attached.
    – vivek_23
    Nov 13 at 8:04










  • if there are still b's left, attach them at the beginning and end of the string.
    – vivek_23
    Nov 13 at 8:34
















-2














Asked in coding challenge.



Example:



A = 5 and B = 3 return "aabaabab" or "abbaabaa" both are correct answers
A = 1 and B = 4 return "bbabb"
constraints : ignore cases where A = 0 and B >= 3 vice-versa









share|improve this question






















  • Seems there is something missing in the question, or is this it?
    – Surt
    Nov 13 at 6:20






  • 1




    So, what is your problem?
    – Andreas
    Nov 13 at 6:28










  • This is it. We need to write a function that will return string like above
    – Shailesh Shekhawat
    Nov 13 at 6:30






  • 1




    Compare a and b values. Let's say if a is larger, then create a string with only a's and then insert b after every 2 a's. If there are still b's left, then insert b after b that was attached.
    – vivek_23
    Nov 13 at 8:04










  • if there are still b's left, attach them at the beginning and end of the string.
    – vivek_23
    Nov 13 at 8:34














-2












-2








-2







Asked in coding challenge.



Example:



A = 5 and B = 3 return "aabaabab" or "abbaabaa" both are correct answers
A = 1 and B = 4 return "bbabb"
constraints : ignore cases where A = 0 and B >= 3 vice-versa









share|improve this question













Asked in coding challenge.



Example:



A = 5 and B = 3 return "aabaabab" or "abbaabaa" both are correct answers
A = 1 and B = 4 return "bbabb"
constraints : ignore cases where A = 0 and B >= 3 vice-versa






string algorithm data-structures greedy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 at 6:18









Shailesh Shekhawat

135113




135113












  • Seems there is something missing in the question, or is this it?
    – Surt
    Nov 13 at 6:20






  • 1




    So, what is your problem?
    – Andreas
    Nov 13 at 6:28










  • This is it. We need to write a function that will return string like above
    – Shailesh Shekhawat
    Nov 13 at 6:30






  • 1




    Compare a and b values. Let's say if a is larger, then create a string with only a's and then insert b after every 2 a's. If there are still b's left, then insert b after b that was attached.
    – vivek_23
    Nov 13 at 8:04










  • if there are still b's left, attach them at the beginning and end of the string.
    – vivek_23
    Nov 13 at 8:34


















  • Seems there is something missing in the question, or is this it?
    – Surt
    Nov 13 at 6:20






  • 1




    So, what is your problem?
    – Andreas
    Nov 13 at 6:28










  • This is it. We need to write a function that will return string like above
    – Shailesh Shekhawat
    Nov 13 at 6:30






  • 1




    Compare a and b values. Let's say if a is larger, then create a string with only a's and then insert b after every 2 a's. If there are still b's left, then insert b after b that was attached.
    – vivek_23
    Nov 13 at 8:04










  • if there are still b's left, attach them at the beginning and end of the string.
    – vivek_23
    Nov 13 at 8:34
















Seems there is something missing in the question, or is this it?
– Surt
Nov 13 at 6:20




Seems there is something missing in the question, or is this it?
– Surt
Nov 13 at 6:20




1




1




So, what is your problem?
– Andreas
Nov 13 at 6:28




So, what is your problem?
– Andreas
Nov 13 at 6:28












This is it. We need to write a function that will return string like above
– Shailesh Shekhawat
Nov 13 at 6:30




This is it. We need to write a function that will return string like above
– Shailesh Shekhawat
Nov 13 at 6:30




1




1




Compare a and b values. Let's say if a is larger, then create a string with only a's and then insert b after every 2 a's. If there are still b's left, then insert b after b that was attached.
– vivek_23
Nov 13 at 8:04




Compare a and b values. Let's say if a is larger, then create a string with only a's and then insert b after every 2 a's. If there are still b's left, then insert b after b that was attached.
– vivek_23
Nov 13 at 8:04












if there are still b's left, attach them at the beginning and end of the string.
– vivek_23
Nov 13 at 8:34




if there are still b's left, attach them at the beginning and end of the string.
– vivek_23
Nov 13 at 8:34












2 Answers
2






active

oldest

votes


















2














As per above problem my JAVA solution is:



public static String solution(int A, int B) {

String biggerString, smallerString;
int biggerCount, smallerCount;

if (A >= B) {
biggerString = "a";
smallerString = "b";
biggerCount = A;
smallerCount = B;
} else {
biggerString = "b";
smallerString = "a";
biggerCount = B;
smallerCount = A;
}

// A > 2B + 2
if (biggerCount > 2 * smallerCount + 2) {
return "No Soulution";
}

StringBuilder returnString = new StringBuilder();

do {

if (biggerCount == smallerCount + 1) {

returnString.append(biggerString);
biggerCount--;

} else if (biggerCount == smallerCount) {
returnString.append(biggerString);
biggerCount--;

returnString.append(smallerString);
smallerCount--;

} else {

returnString.append(biggerString);
returnString.append(biggerString);
biggerCount -= 2;

if (smallerCount > 0) {
returnString.append(smallerString);
smallerCount -= 1;
}
}

} while (biggerCount > 0);

return returnString.toString();

}





share|improve this answer





























    1














    Assume A is greater than B, otherwise swap them (keep the track of which belongs to which letter).



    If A > 2B + 2, there is no solution, as you don't have enough letters b to fill the gaps between a's.



    Keep inserting 2 a's and 1 b's until there is equal number of them or there is no more b's. After that place 1 of each consecutively.
    At the end you'll be left with no more letters, or 1 more letter a. Place it at the end. You're done.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      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
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274921%2fgiven-two-integers-a-and-b-return-string-containing-letters-a-and-b-with-no%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














      As per above problem my JAVA solution is:



      public static String solution(int A, int B) {

      String biggerString, smallerString;
      int biggerCount, smallerCount;

      if (A >= B) {
      biggerString = "a";
      smallerString = "b";
      biggerCount = A;
      smallerCount = B;
      } else {
      biggerString = "b";
      smallerString = "a";
      biggerCount = B;
      smallerCount = A;
      }

      // A > 2B + 2
      if (biggerCount > 2 * smallerCount + 2) {
      return "No Soulution";
      }

      StringBuilder returnString = new StringBuilder();

      do {

      if (biggerCount == smallerCount + 1) {

      returnString.append(biggerString);
      biggerCount--;

      } else if (biggerCount == smallerCount) {
      returnString.append(biggerString);
      biggerCount--;

      returnString.append(smallerString);
      smallerCount--;

      } else {

      returnString.append(biggerString);
      returnString.append(biggerString);
      biggerCount -= 2;

      if (smallerCount > 0) {
      returnString.append(smallerString);
      smallerCount -= 1;
      }
      }

      } while (biggerCount > 0);

      return returnString.toString();

      }





      share|improve this answer


























        2














        As per above problem my JAVA solution is:



        public static String solution(int A, int B) {

        String biggerString, smallerString;
        int biggerCount, smallerCount;

        if (A >= B) {
        biggerString = "a";
        smallerString = "b";
        biggerCount = A;
        smallerCount = B;
        } else {
        biggerString = "b";
        smallerString = "a";
        biggerCount = B;
        smallerCount = A;
        }

        // A > 2B + 2
        if (biggerCount > 2 * smallerCount + 2) {
        return "No Soulution";
        }

        StringBuilder returnString = new StringBuilder();

        do {

        if (biggerCount == smallerCount + 1) {

        returnString.append(biggerString);
        biggerCount--;

        } else if (biggerCount == smallerCount) {
        returnString.append(biggerString);
        biggerCount--;

        returnString.append(smallerString);
        smallerCount--;

        } else {

        returnString.append(biggerString);
        returnString.append(biggerString);
        biggerCount -= 2;

        if (smallerCount > 0) {
        returnString.append(smallerString);
        smallerCount -= 1;
        }
        }

        } while (biggerCount > 0);

        return returnString.toString();

        }





        share|improve this answer
























          2












          2








          2






          As per above problem my JAVA solution is:



          public static String solution(int A, int B) {

          String biggerString, smallerString;
          int biggerCount, smallerCount;

          if (A >= B) {
          biggerString = "a";
          smallerString = "b";
          biggerCount = A;
          smallerCount = B;
          } else {
          biggerString = "b";
          smallerString = "a";
          biggerCount = B;
          smallerCount = A;
          }

          // A > 2B + 2
          if (biggerCount > 2 * smallerCount + 2) {
          return "No Soulution";
          }

          StringBuilder returnString = new StringBuilder();

          do {

          if (biggerCount == smallerCount + 1) {

          returnString.append(biggerString);
          biggerCount--;

          } else if (biggerCount == smallerCount) {
          returnString.append(biggerString);
          biggerCount--;

          returnString.append(smallerString);
          smallerCount--;

          } else {

          returnString.append(biggerString);
          returnString.append(biggerString);
          biggerCount -= 2;

          if (smallerCount > 0) {
          returnString.append(smallerString);
          smallerCount -= 1;
          }
          }

          } while (biggerCount > 0);

          return returnString.toString();

          }





          share|improve this answer












          As per above problem my JAVA solution is:



          public static String solution(int A, int B) {

          String biggerString, smallerString;
          int biggerCount, smallerCount;

          if (A >= B) {
          biggerString = "a";
          smallerString = "b";
          biggerCount = A;
          smallerCount = B;
          } else {
          biggerString = "b";
          smallerString = "a";
          biggerCount = B;
          smallerCount = A;
          }

          // A > 2B + 2
          if (biggerCount > 2 * smallerCount + 2) {
          return "No Soulution";
          }

          StringBuilder returnString = new StringBuilder();

          do {

          if (biggerCount == smallerCount + 1) {

          returnString.append(biggerString);
          biggerCount--;

          } else if (biggerCount == smallerCount) {
          returnString.append(biggerString);
          biggerCount--;

          returnString.append(smallerString);
          smallerCount--;

          } else {

          returnString.append(biggerString);
          returnString.append(biggerString);
          biggerCount -= 2;

          if (smallerCount > 0) {
          returnString.append(smallerString);
          smallerCount -= 1;
          }
          }

          } while (biggerCount > 0);

          return returnString.toString();

          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 17:41









          Amrith Raj Herle

          1268




          1268

























              1














              Assume A is greater than B, otherwise swap them (keep the track of which belongs to which letter).



              If A > 2B + 2, there is no solution, as you don't have enough letters b to fill the gaps between a's.



              Keep inserting 2 a's and 1 b's until there is equal number of them or there is no more b's. After that place 1 of each consecutively.
              At the end you'll be left with no more letters, or 1 more letter a. Place it at the end. You're done.






              share|improve this answer


























                1














                Assume A is greater than B, otherwise swap them (keep the track of which belongs to which letter).



                If A > 2B + 2, there is no solution, as you don't have enough letters b to fill the gaps between a's.



                Keep inserting 2 a's and 1 b's until there is equal number of them or there is no more b's. After that place 1 of each consecutively.
                At the end you'll be left with no more letters, or 1 more letter a. Place it at the end. You're done.






                share|improve this answer
























                  1












                  1








                  1






                  Assume A is greater than B, otherwise swap them (keep the track of which belongs to which letter).



                  If A > 2B + 2, there is no solution, as you don't have enough letters b to fill the gaps between a's.



                  Keep inserting 2 a's and 1 b's until there is equal number of them or there is no more b's. After that place 1 of each consecutively.
                  At the end you'll be left with no more letters, or 1 more letter a. Place it at the end. You're done.






                  share|improve this answer












                  Assume A is greater than B, otherwise swap them (keep the track of which belongs to which letter).



                  If A > 2B + 2, there is no solution, as you don't have enough letters b to fill the gaps between a's.



                  Keep inserting 2 a's and 1 b's until there is equal number of them or there is no more b's. After that place 1 of each consecutively.
                  At the end you'll be left with no more letters, or 1 more letter a. Place it at the end. You're done.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 at 9:49









                  hazeiio

                  1796




                  1796






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • 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.


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





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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.


                      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%2fstackoverflow.com%2fquestions%2f53274921%2fgiven-two-integers-a-and-b-return-string-containing-letters-a-and-b-with-no%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

                      Contact image not getting when fetch all contact list from iPhone by CNContact

                      count number of partitions of a set with n elements into k subsets

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks