0

I have written a coding challenge. The requirement of the challenge is reversing the particular words in a sentence => still keep the oder the of the words in the sentence but reverse the characters of the word.

The input sample is something like this: RemoteIo is awesome-Candiates pass interview-best candiates are selected.

The sample output of the input above:

oIetomeR si emosewa 
setaidnaC ssap weivretni 
tseb setaidnac era detceles

As you can see the input sentences are separated by the - character so that mean we have 3 sentences in the example above and the sentence just able to contain anphabet characters and blank space only (one blank space between two words)

So my question is how can I optimize the below code and there is any theory/principle about Code Optimization. My code implementation in Java:

public class Test1 {
    public static void main (String[] args) throws java.lang.Exception
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();
        
        // Put sentences to a String array.
        String[] data = input.split("-");
        
        // Loop throw sentence array
        for(int i = 0; i < data.length; i++) {
            
            // Put the words from the sentence to a String array.
            String[] words = data[i].split(" ");
            
            // Loop throw the word array
            for(int w = 0; w < words.length; w++) {
                
                // Revert the characters of each word
                for (int j = words[w].length() - 1;  j >=0; j--) {
                    if (j != 0) {
                        System.out.print(words[w].charAt(j));
                    } else {
                        System.out.print(words[w].charAt(j) + " ");
                    }
                }
                if ( w == words.length -1) {
                    System.out.println();
                }
            }
        }
    }
}
19
  • 2
    It's almost always a bad idea to mix printing commands in with your logic in something like this. Map words to their reverse and then join them with a space, don't try to print spaces as you go, you'll just give yourself a headache. Commented Nov 22, 2022 at 16:37
  • 1
    Do you have a reason to believe your code is insufficiently optimized? To me it looks perfectly reasonable. A good rule is to pick a good algorithm but don't worry about further optimization until you need to. Commented Nov 22, 2022 at 16:39
  • 1
    "And do we have any theory /principle about code optimization." - yes, vast libraries of theory, far too much to cover in an SO question. But no, for performance, there's nothing worthwhile to do here. That's usually the case - simple code tends to be fast, and for low-level stuff, optimization is the compiler's job, not yours. Commented Nov 22, 2022 at 17:20
  • 2
    @HoangNguyen since he's reviewing your code, and probably left a "plesae optimize this part" comment on this piece of code, could you ask him to "could you please clarify, what sort of improvement did you have in mind? what's actually wrong with it?" Commented Nov 22, 2022 at 18:09
  • 2
    To be clear, I don't actually recommend "research all of it" either, I just think that's as good as anything for where you're at. The most important lesson right now is to not be concerned with premature optimization, and to focus more on readability, maintainability and correctness. Commented Nov 22, 2022 at 18:20

1 Answer 1

1

The following code is bad: having ifs that refer to the loop variable is confusing and technically, maybe very slightly slower than the right way (though the compiler might just fix that for you.) // Loop throw the word array for(int w = 0; w < words.length; w++) {

                // Revert the characters of each word
                for (int j = words[w].length() - 1;  j >=0; j--) {
                    if (j != 0) {
                        System.out.print(words[w].charAt(j));
                    } else {
                        System.out.print(words[w].charAt(j) + " ");
                    }
                }
                if ( w == words.length -1) {
                    System.out.println();
                }
            }

Instead do:

  // Loop throw the word array
                for(int w = 0; w < words.length; w++) {
                    // Revert the characters of each word
                    for (int j = words[w].length() - 1;  j >0; j--) {
                        System.out.print(words[w].charAt(j) + " ");
                    }
                    System.out.print(words[w].charAt(j));
                }
                System.out.println();

Notice how I changed the j>=0 to j>0. The else block would only have triggered on the last repetition, so this is semantically equivalent.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.