Repeated Strings Hacker Rank challenge











up vote
3
down vote

favorite












I'm doing a hacker rank challenge with this one called "Repeated String". I attempted this challenge in my weaker language java. The goal is to print out the occurrences of the letter a. First you're given a string "aba" then a number 10. The new string generated would be abaabaabaa and you need to count the a. The solution below works but is it as simple as it should be? I've looked around and my solution looks good but still I'm terrified I'm over complicating this.



// Complete the repeatedString function below.
static long repeatedString(String s, long n) {
final long quotient = n/s.length();
final long remainder = n%s.length();
long aCount = 0;

if((s.length() == 1) && (s.charAt(0) == 'a')){
return n;
}
for(int i=0; i<s.length(); i++){
if(s.charAt(i) == 'a'){
aCount++;
}
}
aCount = aCount * quotient;
for(int i=0; i<remainder; i++){
if(s.charAt(i) == 'a'){
aCount++;
}
}
return aCount;
}









share|improve this question




























    up vote
    3
    down vote

    favorite












    I'm doing a hacker rank challenge with this one called "Repeated String". I attempted this challenge in my weaker language java. The goal is to print out the occurrences of the letter a. First you're given a string "aba" then a number 10. The new string generated would be abaabaabaa and you need to count the a. The solution below works but is it as simple as it should be? I've looked around and my solution looks good but still I'm terrified I'm over complicating this.



    // Complete the repeatedString function below.
    static long repeatedString(String s, long n) {
    final long quotient = n/s.length();
    final long remainder = n%s.length();
    long aCount = 0;

    if((s.length() == 1) && (s.charAt(0) == 'a')){
    return n;
    }
    for(int i=0; i<s.length(); i++){
    if(s.charAt(i) == 'a'){
    aCount++;
    }
    }
    aCount = aCount * quotient;
    for(int i=0; i<remainder; i++){
    if(s.charAt(i) == 'a'){
    aCount++;
    }
    }
    return aCount;
    }









    share|improve this question


























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I'm doing a hacker rank challenge with this one called "Repeated String". I attempted this challenge in my weaker language java. The goal is to print out the occurrences of the letter a. First you're given a string "aba" then a number 10. The new string generated would be abaabaabaa and you need to count the a. The solution below works but is it as simple as it should be? I've looked around and my solution looks good but still I'm terrified I'm over complicating this.



      // Complete the repeatedString function below.
      static long repeatedString(String s, long n) {
      final long quotient = n/s.length();
      final long remainder = n%s.length();
      long aCount = 0;

      if((s.length() == 1) && (s.charAt(0) == 'a')){
      return n;
      }
      for(int i=0; i<s.length(); i++){
      if(s.charAt(i) == 'a'){
      aCount++;
      }
      }
      aCount = aCount * quotient;
      for(int i=0; i<remainder; i++){
      if(s.charAt(i) == 'a'){
      aCount++;
      }
      }
      return aCount;
      }









      share|improve this question















      I'm doing a hacker rank challenge with this one called "Repeated String". I attempted this challenge in my weaker language java. The goal is to print out the occurrences of the letter a. First you're given a string "aba" then a number 10. The new string generated would be abaabaabaa and you need to count the a. The solution below works but is it as simple as it should be? I've looked around and my solution looks good but still I'm terrified I'm over complicating this.



      // Complete the repeatedString function below.
      static long repeatedString(String s, long n) {
      final long quotient = n/s.length();
      final long remainder = n%s.length();
      long aCount = 0;

      if((s.length() == 1) && (s.charAt(0) == 'a')){
      return n;
      }
      for(int i=0; i<s.length(); i++){
      if(s.charAt(i) == 'a'){
      aCount++;
      }
      }
      aCount = aCount * quotient;
      for(int i=0; i<remainder; i++){
      if(s.charAt(i) == 'a'){
      aCount++;
      }
      }
      return aCount;
      }






      java strings programming-challenge






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday









      200_success

      127k15148410




      127k15148410










      asked yesterday









      Laurent

      434




      434






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          If your goal is to optimize efficiency, you can do a little better by only looking at each character in the string once. The first remainder characters of the string will occur quotient + 1 times. The remaining characters will occur quotient times. So you can loop once from 0 to remainder and do some math, then loop from remainder + 1 to s.length() and do some math, and you'll have your result. It's unclear without testing whether it's faster to do the multiplication outside or to add the correct amount in your loop. Also, your short-circuit for inputs of length one is not helpful unless you expect that most of the inputs will be a single-character. It's probably safe to pull it.



          If your goal is to avoid overcomplicating things, websites like HackerRank may not be ideal. They tend to provide algorithmic challenges that reward hyper-optimized code. By their nature, those kinds of solutions will be complicated. It turns out it's hard to write an AI that will grade readable code, but easy to write one that will grade performant code. So even though readable code is much more desirable, performant code is what scores well.






          share|improve this answer





















          • If I could, I'd give +10 to this answer for the comment about overcomplicating and unnecessary hyper-optimizing code.
            – mtj
            yesterday











          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.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          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',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fcodereview.stackexchange.com%2fquestions%2f208034%2frepeated-strings-hacker-rank-challenge%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote













          If your goal is to optimize efficiency, you can do a little better by only looking at each character in the string once. The first remainder characters of the string will occur quotient + 1 times. The remaining characters will occur quotient times. So you can loop once from 0 to remainder and do some math, then loop from remainder + 1 to s.length() and do some math, and you'll have your result. It's unclear without testing whether it's faster to do the multiplication outside or to add the correct amount in your loop. Also, your short-circuit for inputs of length one is not helpful unless you expect that most of the inputs will be a single-character. It's probably safe to pull it.



          If your goal is to avoid overcomplicating things, websites like HackerRank may not be ideal. They tend to provide algorithmic challenges that reward hyper-optimized code. By their nature, those kinds of solutions will be complicated. It turns out it's hard to write an AI that will grade readable code, but easy to write one that will grade performant code. So even though readable code is much more desirable, performant code is what scores well.






          share|improve this answer





















          • If I could, I'd give +10 to this answer for the comment about overcomplicating and unnecessary hyper-optimizing code.
            – mtj
            yesterday















          up vote
          3
          down vote













          If your goal is to optimize efficiency, you can do a little better by only looking at each character in the string once. The first remainder characters of the string will occur quotient + 1 times. The remaining characters will occur quotient times. So you can loop once from 0 to remainder and do some math, then loop from remainder + 1 to s.length() and do some math, and you'll have your result. It's unclear without testing whether it's faster to do the multiplication outside or to add the correct amount in your loop. Also, your short-circuit for inputs of length one is not helpful unless you expect that most of the inputs will be a single-character. It's probably safe to pull it.



          If your goal is to avoid overcomplicating things, websites like HackerRank may not be ideal. They tend to provide algorithmic challenges that reward hyper-optimized code. By their nature, those kinds of solutions will be complicated. It turns out it's hard to write an AI that will grade readable code, but easy to write one that will grade performant code. So even though readable code is much more desirable, performant code is what scores well.






          share|improve this answer





















          • If I could, I'd give +10 to this answer for the comment about overcomplicating and unnecessary hyper-optimizing code.
            – mtj
            yesterday













          up vote
          3
          down vote










          up vote
          3
          down vote









          If your goal is to optimize efficiency, you can do a little better by only looking at each character in the string once. The first remainder characters of the string will occur quotient + 1 times. The remaining characters will occur quotient times. So you can loop once from 0 to remainder and do some math, then loop from remainder + 1 to s.length() and do some math, and you'll have your result. It's unclear without testing whether it's faster to do the multiplication outside or to add the correct amount in your loop. Also, your short-circuit for inputs of length one is not helpful unless you expect that most of the inputs will be a single-character. It's probably safe to pull it.



          If your goal is to avoid overcomplicating things, websites like HackerRank may not be ideal. They tend to provide algorithmic challenges that reward hyper-optimized code. By their nature, those kinds of solutions will be complicated. It turns out it's hard to write an AI that will grade readable code, but easy to write one that will grade performant code. So even though readable code is much more desirable, performant code is what scores well.






          share|improve this answer












          If your goal is to optimize efficiency, you can do a little better by only looking at each character in the string once. The first remainder characters of the string will occur quotient + 1 times. The remaining characters will occur quotient times. So you can loop once from 0 to remainder and do some math, then loop from remainder + 1 to s.length() and do some math, and you'll have your result. It's unclear without testing whether it's faster to do the multiplication outside or to add the correct amount in your loop. Also, your short-circuit for inputs of length one is not helpful unless you expect that most of the inputs will be a single-character. It's probably safe to pull it.



          If your goal is to avoid overcomplicating things, websites like HackerRank may not be ideal. They tend to provide algorithmic challenges that reward hyper-optimized code. By their nature, those kinds of solutions will be complicated. It turns out it's hard to write an AI that will grade readable code, but easy to write one that will grade performant code. So even though readable code is much more desirable, performant code is what scores well.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          Eric Stein

          4,003513




          4,003513












          • If I could, I'd give +10 to this answer for the comment about overcomplicating and unnecessary hyper-optimizing code.
            – mtj
            yesterday


















          • If I could, I'd give +10 to this answer for the comment about overcomplicating and unnecessary hyper-optimizing code.
            – mtj
            yesterday
















          If I could, I'd give +10 to this answer for the comment about overcomplicating and unnecessary hyper-optimizing code.
          – mtj
          yesterday




          If I could, I'd give +10 to this answer for the comment about overcomplicating and unnecessary hyper-optimizing code.
          – mtj
          yesterday


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208034%2frepeated-strings-hacker-rank-challenge%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

          Costa Masnaga

          Fotorealismo

          Sidney Franklin