Java 5ms recursive scan once solution


  • 0
    F
    public class Solution {
        
        private String aux(String s, int i){
            for(int k = i; k < s.length(); k++){
                if(s.charAt(k) == '?' && s.charAt(k-1) == 'T'){
                    return aux(s, k + 1);
                }else if(s.charAt(k) == '?' && s.charAt(k-1) == 'F'){
                    int t = 1;
                    while(t != 0){
                        char c = s.charAt(++k);
                        if(c == '?') t++;
                        else if(c == ':')t--;
                    }
                    return aux(s, k + 1);
                }else if(s.charAt(k) == ':'){
                    return s.substring(i, k);
                }
            }
            return s.substring(i);
        }
        
        public String parseTernary(String s) {
            return aux(s, 0);        
        }
    }
    

Log in to reply
 

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