Sharing my AC JAVA solution


  • 0
    O
    public class Solution {
        public   String reverseWords(String s) {
           return helper(s,true);
        }
    	public  String helper(String s , boolean isFirst)
    	{
    		if(s.length()==0)
            	return s;
            else {
            	String result="";
            	int i ;
            	for( i=s.length()-1;i>-1;i--)
            	{
            		if(s.charAt(i)!=' ')
            			result=s.charAt(i)+result;
            		else if(result.equals("")&&isFirst)
                		return result+helper(s.substring(0,i),true);
                		else if(result.equals("")&& !isFirst)
                		return result+helper(s.substring(0,i),false);
                	else if(isFirst)
                		return result+helper(s.substring(0,i),false);
                	else return " "+result+helper(s.substring(0,i),false);
            	}
            	if(isFirst)
            		return result;
            	else return " "+result;
            	
            }
    	}
    }

Log in to reply
 

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