Why time limit exceeded in O(n) solution?


  • 0
    N
    public class Solution {
        public String reverseString(String s) {
            
            Stack<Character> mystack = new Stack<Character>();
            char[] myarr = s.toCharArray();
            String ans = "";
    
            for(int i = 0 ; i < s.length(); i++){            
                mystack.push(myarr[i]);            
            }
                   
            for (int j = 0; j < s.length(); j++){            
                ans = ans + mystack.pop();            
            }
            
            return ans;
          }    
    }
    
    

    This is an O(n) solution but I still don't understand why time limit is exceeding. While other people's O(n) solutions are working perfectly fine. Can anyone explain ?


Log in to reply
 

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