why is my code inefficient?


  • 1
    J
    public class Codec {
    
        // Encodes a list of strings to a single string.
        public String encode(List<String> strs) {
            String result = "";
            for(String s : strs){
               result += encodeSingle(s) + " ";
            }
            System.out.println("ENCODE RESULT" + result + "FCUK");
             System.out.println("ENCODE RESULT  FCUK");
            return result;
        }
    
        // Decodes a single string to a list of strings.
        public List<String> decode(String s) {
            ArrayList<String> result = new ArrayList<String>();
            int marker = 0;
            for(int i=0; i <s.length();i++){
                if(s.charAt(i) == ' '){
                      result.add(decodeSingle(s.substring(marker,i)));
                      marker = i+1;
                      System.out.println("DECODE RESULT" + result);
                      
                }
            }
            return result;
        }
        
        public String encodeSingle (String s){
    		String result = "";
    		for(int i=0; i < s.length(); i++){
    			char cha = (char)(s.charAt(i) + 1);
    			result += cha;
    		}
    		return result;
    	}
    	
    	public static String decodeSingle (String s){
    		String result = "";
    		for(int i=0; i < s.length(); i++){
    			char cha = (char)(s.charAt(i) - 1);
    			result += cha;
    		}
    		return result;
    	}
    }
    

Log in to reply
 

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