Reverse a sentence in Java

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












You are given an array of characters arr that consists of sequences of characters separated by space characters. Each space-delimited sequence of characters defines a word.



Implement a function reverseWords that reverses the order of the words in the array in the most efficient manner.



Explain your solution and analyze its time and space complexities.



Example:

input: arr = [ 'p', 'e', 'r', 'f', 'e', 'c', 't', ' ',
'm', 'a', 'k', 'e', 's', ' ',
'p', 'r', 'a', 'c', 't', 'i', 'c', 'e' ]

output: [ 'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ',
'm', 'a', 'k', 'e', 's', ' ',
'p', 'e', 'r', 'f', 'e', 'c', 't' ]

Constraints:

[time limit] 5000ms

[input] array.character arr

0 ≤ arr.length ≤ 100
[output] array.character


My approach:



import java.io.*;

import java.util.*;

class Solution

static void reverseAword(char arr, int start, int end)

int len = end - start + 1;
for( int i = start, j = end; i < len/2; i++,j-- )

char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;



static char reverseWords(char arr)
// your code goes here
int len = arr.length;

//Reverse the complete sentence
reverseAword(arr,0,len - 1);

int start = 0;
for( int i = 0; i < len; i++)

//If there is a space, reverse the word from start till index - 1( before ' ')
if( arr[i] == ' ' )

if( start == 0 )

reverseAword(arr,start,i - 1);
start = 0;


else if( i == len - 1)

if( start != 0)
reverseAword(arr, start,i);

else

if( start == 0)
start = i;




return arr;


public static void main(String args)

char c1 = ' ', ' ';
c1 = reverseWords(c1);

System.out.println(Arrays.toString(c1));





I have the following questions with respect to the above code:



1) How can I further improve the time and space complexity?



2) Can this problem be solved using a smarter way?



3) Are there too many redundant variables that we are better off with?



Reference







share|improve this question



























    up vote
    1
    down vote

    favorite












    You are given an array of characters arr that consists of sequences of characters separated by space characters. Each space-delimited sequence of characters defines a word.



    Implement a function reverseWords that reverses the order of the words in the array in the most efficient manner.



    Explain your solution and analyze its time and space complexities.



    Example:

    input: arr = [ 'p', 'e', 'r', 'f', 'e', 'c', 't', ' ',
    'm', 'a', 'k', 'e', 's', ' ',
    'p', 'r', 'a', 'c', 't', 'i', 'c', 'e' ]

    output: [ 'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ',
    'm', 'a', 'k', 'e', 's', ' ',
    'p', 'e', 'r', 'f', 'e', 'c', 't' ]

    Constraints:

    [time limit] 5000ms

    [input] array.character arr

    0 ≤ arr.length ≤ 100
    [output] array.character


    My approach:



    import java.io.*;

    import java.util.*;

    class Solution

    static void reverseAword(char arr, int start, int end)

    int len = end - start + 1;
    for( int i = start, j = end; i < len/2; i++,j-- )

    char temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;



    static char reverseWords(char arr)
    // your code goes here
    int len = arr.length;

    //Reverse the complete sentence
    reverseAword(arr,0,len - 1);

    int start = 0;
    for( int i = 0; i < len; i++)

    //If there is a space, reverse the word from start till index - 1( before ' ')
    if( arr[i] == ' ' )

    if( start == 0 )

    reverseAword(arr,start,i - 1);
    start = 0;


    else if( i == len - 1)

    if( start != 0)
    reverseAword(arr, start,i);

    else

    if( start == 0)
    start = i;




    return arr;


    public static void main(String args)

    char c1 = ' ', ' ';
    c1 = reverseWords(c1);

    System.out.println(Arrays.toString(c1));





    I have the following questions with respect to the above code:



    1) How can I further improve the time and space complexity?



    2) Can this problem be solved using a smarter way?



    3) Are there too many redundant variables that we are better off with?



    Reference







    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      You are given an array of characters arr that consists of sequences of characters separated by space characters. Each space-delimited sequence of characters defines a word.



      Implement a function reverseWords that reverses the order of the words in the array in the most efficient manner.



      Explain your solution and analyze its time and space complexities.



      Example:

      input: arr = [ 'p', 'e', 'r', 'f', 'e', 'c', 't', ' ',
      'm', 'a', 'k', 'e', 's', ' ',
      'p', 'r', 'a', 'c', 't', 'i', 'c', 'e' ]

      output: [ 'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ',
      'm', 'a', 'k', 'e', 's', ' ',
      'p', 'e', 'r', 'f', 'e', 'c', 't' ]

      Constraints:

      [time limit] 5000ms

      [input] array.character arr

      0 ≤ arr.length ≤ 100
      [output] array.character


      My approach:



      import java.io.*;

      import java.util.*;

      class Solution

      static void reverseAword(char arr, int start, int end)

      int len = end - start + 1;
      for( int i = start, j = end; i < len/2; i++,j-- )

      char temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;



      static char reverseWords(char arr)
      // your code goes here
      int len = arr.length;

      //Reverse the complete sentence
      reverseAword(arr,0,len - 1);

      int start = 0;
      for( int i = 0; i < len; i++)

      //If there is a space, reverse the word from start till index - 1( before ' ')
      if( arr[i] == ' ' )

      if( start == 0 )

      reverseAword(arr,start,i - 1);
      start = 0;


      else if( i == len - 1)

      if( start != 0)
      reverseAword(arr, start,i);

      else

      if( start == 0)
      start = i;




      return arr;


      public static void main(String args)

      char c1 = ' ', ' ';
      c1 = reverseWords(c1);

      System.out.println(Arrays.toString(c1));





      I have the following questions with respect to the above code:



      1) How can I further improve the time and space complexity?



      2) Can this problem be solved using a smarter way?



      3) Are there too many redundant variables that we are better off with?



      Reference







      share|improve this question













      You are given an array of characters arr that consists of sequences of characters separated by space characters. Each space-delimited sequence of characters defines a word.



      Implement a function reverseWords that reverses the order of the words in the array in the most efficient manner.



      Explain your solution and analyze its time and space complexities.



      Example:

      input: arr = [ 'p', 'e', 'r', 'f', 'e', 'c', 't', ' ',
      'm', 'a', 'k', 'e', 's', ' ',
      'p', 'r', 'a', 'c', 't', 'i', 'c', 'e' ]

      output: [ 'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ',
      'm', 'a', 'k', 'e', 's', ' ',
      'p', 'e', 'r', 'f', 'e', 'c', 't' ]

      Constraints:

      [time limit] 5000ms

      [input] array.character arr

      0 ≤ arr.length ≤ 100
      [output] array.character


      My approach:



      import java.io.*;

      import java.util.*;

      class Solution

      static void reverseAword(char arr, int start, int end)

      int len = end - start + 1;
      for( int i = start, j = end; i < len/2; i++,j-- )

      char temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;



      static char reverseWords(char arr)
      // your code goes here
      int len = arr.length;

      //Reverse the complete sentence
      reverseAword(arr,0,len - 1);

      int start = 0;
      for( int i = 0; i < len; i++)

      //If there is a space, reverse the word from start till index - 1( before ' ')
      if( arr[i] == ' ' )

      if( start == 0 )

      reverseAword(arr,start,i - 1);
      start = 0;


      else if( i == len - 1)

      if( start != 0)
      reverseAword(arr, start,i);

      else

      if( start == 0)
      start = i;




      return arr;


      public static void main(String args)

      char c1 = ' ', ' ';
      c1 = reverseWords(c1);

      System.out.println(Arrays.toString(c1));





      I have the following questions with respect to the above code:



      1) How can I further improve the time and space complexity?



      2) Can this problem be solved using a smarter way?



      3) Are there too many redundant variables that we are better off with?



      Reference









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 4 at 15:32









      yuri

      3,3872832




      3,3872832









      asked Jun 4 at 14:15









      Anirudh Thatipelli

      227210




      227210




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          You can improve time complexity by doubling the space complexity. If you don't modify the input array in place (which is probably a bad idea in real code anyway), you can use System.arraycopy instead of repeatedly reversing parts of the char. I also think that would be easier to read. That approach might look something like:



          import java.util.Arrays;

          public final class Solution

          private static final char INPUT =
          new char
          'p', 'e', 'r', 'f', 'e', 'c', 't', ' ',
          'm', 'a', 'k', 'e', 's', ' ',
          'p', 'r', 'a', 'c', 't', 'i', 'c', 'e' ;

          private static final char OUTPUT =
          new char
          'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ',
          'm', 'a', 'k', 'e', 's', ' ',
          'p', 'e', 'r', 'f', 'e', 'c', 't' ;


          public static void main(final String argv)
          System.out.println(Arrays.equals(OUTPUT, reverseWords(INPUT)));


          private static char reverseWords(final char input)
          final char output = new char[input.length];
          int outputIndex = 0;
          int lastSpaceIndex = input.length;
          for (int i = input.length - 1; i >= 0; i--)
          if (input[i] == ' ')
          final int length = lastSpaceIndex - (i + 1);
          System.arraycopy(input, i + 1, output, outputIndex, length);
          output[outputIndex + length] = ' ';
          outputIndex += length + 1;
          lastSpaceIndex = i;


          System.arraycopy(input, 0, output, outputIndex, lastSpaceIndex);
          return output;







          share|improve this answer





















          • This code is so elegant. Thanks for sharing it, @Eric Stein.
            – Anirudh Thatipelli
            Jun 6 at 17:25

















          up vote
          4
          down vote













          This solution sacrifices both time and space, for a very readable solution. In practice it's bad to prematurely optimize and always good to maximize readability before starting to optimize.



          static char reverseWords(char arr) 
          String sentence = String.valueOf(arr);
          List<String> words = Arrays.asList(sentence.split(" "));

          Collections.reverse(words);

          return String.join(" ", words).toCharArray();






          share|improve this answer





















          • +1 for an elegant solution. Depending on the nature of the interview problem, that may or may not count - but it definitely should! OP is working on an online, automated code-submission website. If OP is practicing for the real world, this is definitely the code to use, but it may not pass the automated test framework's performance requirements.
            – Eric Stein
            Jun 7 at 10:34










          • I missed that part. But hey, my point still stands. If the performance is bad, then and only then it's time to optimize. If you're in the middle of a timed programming contest, then you're in a whole different league though.
            – Mark Jeronimus
            Jun 7 at 11:22










          • Thanks for sharing the advice and code @MarkJeronimus. As Eric Stein has rightly mentioned, I am working on an online interview website and preparing for real life coding interviews too. My aim is to keep the code succinct and follow all the java coding conventions.
            – Anirudh Thatipelli
            Jun 8 at 9:59

















          up vote
          1
          down vote













          With regard to 1) : No, it can't. You algorithm already works in-place and O(n), so it is impossible to improve the time or space complexity, at most you can improve constant factors. I argue that the lower time complexity bound is O(n) because at the very least, you have to look at the complete array to find all the spaces.






          share|improve this answer





















            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: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f195815%2freverse-a-sentence-in-java%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote



            accepted










            You can improve time complexity by doubling the space complexity. If you don't modify the input array in place (which is probably a bad idea in real code anyway), you can use System.arraycopy instead of repeatedly reversing parts of the char. I also think that would be easier to read. That approach might look something like:



            import java.util.Arrays;

            public final class Solution

            private static final char INPUT =
            new char
            'p', 'e', 'r', 'f', 'e', 'c', 't', ' ',
            'm', 'a', 'k', 'e', 's', ' ',
            'p', 'r', 'a', 'c', 't', 'i', 'c', 'e' ;

            private static final char OUTPUT =
            new char
            'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ',
            'm', 'a', 'k', 'e', 's', ' ',
            'p', 'e', 'r', 'f', 'e', 'c', 't' ;


            public static void main(final String argv)
            System.out.println(Arrays.equals(OUTPUT, reverseWords(INPUT)));


            private static char reverseWords(final char input)
            final char output = new char[input.length];
            int outputIndex = 0;
            int lastSpaceIndex = input.length;
            for (int i = input.length - 1; i >= 0; i--)
            if (input[i] == ' ')
            final int length = lastSpaceIndex - (i + 1);
            System.arraycopy(input, i + 1, output, outputIndex, length);
            output[outputIndex + length] = ' ';
            outputIndex += length + 1;
            lastSpaceIndex = i;


            System.arraycopy(input, 0, output, outputIndex, lastSpaceIndex);
            return output;







            share|improve this answer





















            • This code is so elegant. Thanks for sharing it, @Eric Stein.
              – Anirudh Thatipelli
              Jun 6 at 17:25














            up vote
            4
            down vote



            accepted










            You can improve time complexity by doubling the space complexity. If you don't modify the input array in place (which is probably a bad idea in real code anyway), you can use System.arraycopy instead of repeatedly reversing parts of the char. I also think that would be easier to read. That approach might look something like:



            import java.util.Arrays;

            public final class Solution

            private static final char INPUT =
            new char
            'p', 'e', 'r', 'f', 'e', 'c', 't', ' ',
            'm', 'a', 'k', 'e', 's', ' ',
            'p', 'r', 'a', 'c', 't', 'i', 'c', 'e' ;

            private static final char OUTPUT =
            new char
            'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ',
            'm', 'a', 'k', 'e', 's', ' ',
            'p', 'e', 'r', 'f', 'e', 'c', 't' ;


            public static void main(final String argv)
            System.out.println(Arrays.equals(OUTPUT, reverseWords(INPUT)));


            private static char reverseWords(final char input)
            final char output = new char[input.length];
            int outputIndex = 0;
            int lastSpaceIndex = input.length;
            for (int i = input.length - 1; i >= 0; i--)
            if (input[i] == ' ')
            final int length = lastSpaceIndex - (i + 1);
            System.arraycopy(input, i + 1, output, outputIndex, length);
            output[outputIndex + length] = ' ';
            outputIndex += length + 1;
            lastSpaceIndex = i;


            System.arraycopy(input, 0, output, outputIndex, lastSpaceIndex);
            return output;







            share|improve this answer





















            • This code is so elegant. Thanks for sharing it, @Eric Stein.
              – Anirudh Thatipelli
              Jun 6 at 17:25












            up vote
            4
            down vote



            accepted







            up vote
            4
            down vote



            accepted






            You can improve time complexity by doubling the space complexity. If you don't modify the input array in place (which is probably a bad idea in real code anyway), you can use System.arraycopy instead of repeatedly reversing parts of the char. I also think that would be easier to read. That approach might look something like:



            import java.util.Arrays;

            public final class Solution

            private static final char INPUT =
            new char
            'p', 'e', 'r', 'f', 'e', 'c', 't', ' ',
            'm', 'a', 'k', 'e', 's', ' ',
            'p', 'r', 'a', 'c', 't', 'i', 'c', 'e' ;

            private static final char OUTPUT =
            new char
            'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ',
            'm', 'a', 'k', 'e', 's', ' ',
            'p', 'e', 'r', 'f', 'e', 'c', 't' ;


            public static void main(final String argv)
            System.out.println(Arrays.equals(OUTPUT, reverseWords(INPUT)));


            private static char reverseWords(final char input)
            final char output = new char[input.length];
            int outputIndex = 0;
            int lastSpaceIndex = input.length;
            for (int i = input.length - 1; i >= 0; i--)
            if (input[i] == ' ')
            final int length = lastSpaceIndex - (i + 1);
            System.arraycopy(input, i + 1, output, outputIndex, length);
            output[outputIndex + length] = ' ';
            outputIndex += length + 1;
            lastSpaceIndex = i;


            System.arraycopy(input, 0, output, outputIndex, lastSpaceIndex);
            return output;







            share|improve this answer













            You can improve time complexity by doubling the space complexity. If you don't modify the input array in place (which is probably a bad idea in real code anyway), you can use System.arraycopy instead of repeatedly reversing parts of the char. I also think that would be easier to read. That approach might look something like:



            import java.util.Arrays;

            public final class Solution

            private static final char INPUT =
            new char
            'p', 'e', 'r', 'f', 'e', 'c', 't', ' ',
            'm', 'a', 'k', 'e', 's', ' ',
            'p', 'r', 'a', 'c', 't', 'i', 'c', 'e' ;

            private static final char OUTPUT =
            new char
            'p', 'r', 'a', 'c', 't', 'i', 'c', 'e', ' ',
            'm', 'a', 'k', 'e', 's', ' ',
            'p', 'e', 'r', 'f', 'e', 'c', 't' ;


            public static void main(final String argv)
            System.out.println(Arrays.equals(OUTPUT, reverseWords(INPUT)));


            private static char reverseWords(final char input)
            final char output = new char[input.length];
            int outputIndex = 0;
            int lastSpaceIndex = input.length;
            for (int i = input.length - 1; i >= 0; i--)
            if (input[i] == ' ')
            final int length = lastSpaceIndex - (i + 1);
            System.arraycopy(input, i + 1, output, outputIndex, length);
            output[outputIndex + length] = ' ';
            outputIndex += length + 1;
            lastSpaceIndex = i;


            System.arraycopy(input, 0, output, outputIndex, lastSpaceIndex);
            return output;








            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Jun 5 at 13:34









            Eric Stein

            3,658512




            3,658512











            • This code is so elegant. Thanks for sharing it, @Eric Stein.
              – Anirudh Thatipelli
              Jun 6 at 17:25
















            • This code is so elegant. Thanks for sharing it, @Eric Stein.
              – Anirudh Thatipelli
              Jun 6 at 17:25















            This code is so elegant. Thanks for sharing it, @Eric Stein.
            – Anirudh Thatipelli
            Jun 6 at 17:25




            This code is so elegant. Thanks for sharing it, @Eric Stein.
            – Anirudh Thatipelli
            Jun 6 at 17:25












            up vote
            4
            down vote













            This solution sacrifices both time and space, for a very readable solution. In practice it's bad to prematurely optimize and always good to maximize readability before starting to optimize.



            static char reverseWords(char arr) 
            String sentence = String.valueOf(arr);
            List<String> words = Arrays.asList(sentence.split(" "));

            Collections.reverse(words);

            return String.join(" ", words).toCharArray();






            share|improve this answer





















            • +1 for an elegant solution. Depending on the nature of the interview problem, that may or may not count - but it definitely should! OP is working on an online, automated code-submission website. If OP is practicing for the real world, this is definitely the code to use, but it may not pass the automated test framework's performance requirements.
              – Eric Stein
              Jun 7 at 10:34










            • I missed that part. But hey, my point still stands. If the performance is bad, then and only then it's time to optimize. If you're in the middle of a timed programming contest, then you're in a whole different league though.
              – Mark Jeronimus
              Jun 7 at 11:22










            • Thanks for sharing the advice and code @MarkJeronimus. As Eric Stein has rightly mentioned, I am working on an online interview website and preparing for real life coding interviews too. My aim is to keep the code succinct and follow all the java coding conventions.
              – Anirudh Thatipelli
              Jun 8 at 9:59














            up vote
            4
            down vote













            This solution sacrifices both time and space, for a very readable solution. In practice it's bad to prematurely optimize and always good to maximize readability before starting to optimize.



            static char reverseWords(char arr) 
            String sentence = String.valueOf(arr);
            List<String> words = Arrays.asList(sentence.split(" "));

            Collections.reverse(words);

            return String.join(" ", words).toCharArray();






            share|improve this answer





















            • +1 for an elegant solution. Depending on the nature of the interview problem, that may or may not count - but it definitely should! OP is working on an online, automated code-submission website. If OP is practicing for the real world, this is definitely the code to use, but it may not pass the automated test framework's performance requirements.
              – Eric Stein
              Jun 7 at 10:34










            • I missed that part. But hey, my point still stands. If the performance is bad, then and only then it's time to optimize. If you're in the middle of a timed programming contest, then you're in a whole different league though.
              – Mark Jeronimus
              Jun 7 at 11:22










            • Thanks for sharing the advice and code @MarkJeronimus. As Eric Stein has rightly mentioned, I am working on an online interview website and preparing for real life coding interviews too. My aim is to keep the code succinct and follow all the java coding conventions.
              – Anirudh Thatipelli
              Jun 8 at 9:59












            up vote
            4
            down vote










            up vote
            4
            down vote









            This solution sacrifices both time and space, for a very readable solution. In practice it's bad to prematurely optimize and always good to maximize readability before starting to optimize.



            static char reverseWords(char arr) 
            String sentence = String.valueOf(arr);
            List<String> words = Arrays.asList(sentence.split(" "));

            Collections.reverse(words);

            return String.join(" ", words).toCharArray();






            share|improve this answer













            This solution sacrifices both time and space, for a very readable solution. In practice it's bad to prematurely optimize and always good to maximize readability before starting to optimize.



            static char reverseWords(char arr) 
            String sentence = String.valueOf(arr);
            List<String> words = Arrays.asList(sentence.split(" "));

            Collections.reverse(words);

            return String.join(" ", words).toCharArray();







            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Jun 7 at 8:30









            Mark Jeronimus

            1865




            1865











            • +1 for an elegant solution. Depending on the nature of the interview problem, that may or may not count - but it definitely should! OP is working on an online, automated code-submission website. If OP is practicing for the real world, this is definitely the code to use, but it may not pass the automated test framework's performance requirements.
              – Eric Stein
              Jun 7 at 10:34










            • I missed that part. But hey, my point still stands. If the performance is bad, then and only then it's time to optimize. If you're in the middle of a timed programming contest, then you're in a whole different league though.
              – Mark Jeronimus
              Jun 7 at 11:22










            • Thanks for sharing the advice and code @MarkJeronimus. As Eric Stein has rightly mentioned, I am working on an online interview website and preparing for real life coding interviews too. My aim is to keep the code succinct and follow all the java coding conventions.
              – Anirudh Thatipelli
              Jun 8 at 9:59
















            • +1 for an elegant solution. Depending on the nature of the interview problem, that may or may not count - but it definitely should! OP is working on an online, automated code-submission website. If OP is practicing for the real world, this is definitely the code to use, but it may not pass the automated test framework's performance requirements.
              – Eric Stein
              Jun 7 at 10:34










            • I missed that part. But hey, my point still stands. If the performance is bad, then and only then it's time to optimize. If you're in the middle of a timed programming contest, then you're in a whole different league though.
              – Mark Jeronimus
              Jun 7 at 11:22










            • Thanks for sharing the advice and code @MarkJeronimus. As Eric Stein has rightly mentioned, I am working on an online interview website and preparing for real life coding interviews too. My aim is to keep the code succinct and follow all the java coding conventions.
              – Anirudh Thatipelli
              Jun 8 at 9:59















            +1 for an elegant solution. Depending on the nature of the interview problem, that may or may not count - but it definitely should! OP is working on an online, automated code-submission website. If OP is practicing for the real world, this is definitely the code to use, but it may not pass the automated test framework's performance requirements.
            – Eric Stein
            Jun 7 at 10:34




            +1 for an elegant solution. Depending on the nature of the interview problem, that may or may not count - but it definitely should! OP is working on an online, automated code-submission website. If OP is practicing for the real world, this is definitely the code to use, but it may not pass the automated test framework's performance requirements.
            – Eric Stein
            Jun 7 at 10:34












            I missed that part. But hey, my point still stands. If the performance is bad, then and only then it's time to optimize. If you're in the middle of a timed programming contest, then you're in a whole different league though.
            – Mark Jeronimus
            Jun 7 at 11:22




            I missed that part. But hey, my point still stands. If the performance is bad, then and only then it's time to optimize. If you're in the middle of a timed programming contest, then you're in a whole different league though.
            – Mark Jeronimus
            Jun 7 at 11:22












            Thanks for sharing the advice and code @MarkJeronimus. As Eric Stein has rightly mentioned, I am working on an online interview website and preparing for real life coding interviews too. My aim is to keep the code succinct and follow all the java coding conventions.
            – Anirudh Thatipelli
            Jun 8 at 9:59




            Thanks for sharing the advice and code @MarkJeronimus. As Eric Stein has rightly mentioned, I am working on an online interview website and preparing for real life coding interviews too. My aim is to keep the code succinct and follow all the java coding conventions.
            – Anirudh Thatipelli
            Jun 8 at 9:59










            up vote
            1
            down vote













            With regard to 1) : No, it can't. You algorithm already works in-place and O(n), so it is impossible to improve the time or space complexity, at most you can improve constant factors. I argue that the lower time complexity bound is O(n) because at the very least, you have to look at the complete array to find all the spaces.






            share|improve this answer

























              up vote
              1
              down vote













              With regard to 1) : No, it can't. You algorithm already works in-place and O(n), so it is impossible to improve the time or space complexity, at most you can improve constant factors. I argue that the lower time complexity bound is O(n) because at the very least, you have to look at the complete array to find all the spaces.






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                With regard to 1) : No, it can't. You algorithm already works in-place and O(n), so it is impossible to improve the time or space complexity, at most you can improve constant factors. I argue that the lower time complexity bound is O(n) because at the very least, you have to look at the complete array to find all the spaces.






                share|improve this answer













                With regard to 1) : No, it can't. You algorithm already works in-place and O(n), so it is impossible to improve the time or space complexity, at most you can improve constant factors. I argue that the lower time complexity bound is O(n) because at the very least, you have to look at the complete array to find all the spaces.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Jun 4 at 15:43









                kutschkem

                25014




                25014






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195815%2freverse-a-sentence-in-java%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Chat program with C++ and SFML

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    Will my employers contract hold up in court?