My Submission O(n)


  • 0
    S

    '''public class Solution {
    public String shortestPalindrome(String s) {
    if(s == null || s.length() <= 1 || s =="")
    return s;

        int i =0 , j = s.length()-1;
        while(j >= 0){
            if(s.charAt(i)==s.charAt(j))
                i++;
            j--;
        }
        
        if(i == s.length())
            return s;
            
        String suffix = s.substring(i);
        String prefix = new StringBuilder(suffix).reverse().toString();
        String mid = shortestPalindrome(s.substring(0,i));
        
        return prefix+mid+suffix;
    }
    

    }'''


  • 0
    L

    Could you please explain why it is bounded by O(n)?


Log in to reply
 

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