Why this O(n) method exceeds time limit


  • 0
    T

    I believe this method is of O(n). However, I still got the time limit exceeded error message. What's wrong with this? Thanks in advance.

    class Solution {
        public String reverseWords(String s) {
            String[] words = s.split(" ");
            String result = "";
            for (int i = 0; i < words.length; i++) {
                for (int j = words[i].length()-1; j >= 0 ; j--) {
                    result += words[i].substring(j, j+1);
                }
                result += " "; 
                
            }
            return result.trim();
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.