JAVA METHOD EASY UNDERSTAND


  • 0
    L

    class Solution {
    public String findLongestWord(String s, List<String> d) {

    	String max="";
    	for(String str:d)
    	{
    		if(isContains(s,str))
    		{
    			if(max.length()<str.length())
    				max=str;
    			else if(max.length()==str.length())
    			{
    				if(max.compareTo(str)>0)
    					max=str;
    			}
    		}
    	}
    	
    	
    	return max;
    	
    	
    }
    public boolean isContains(String s1,String s2){
    	
    	
    	if(s2.length()>s1.length())
    		return false;
    	
    	if(s1.indexOf(s2.charAt(0))==-1)
    		return false;
    	else
    	{
    		int index=s1.indexOf(s2.charAt(0));
    		String str=s1.substring(index+1,s1.length());
    		for(int i=1;i<s2.length();i++)
    		{
    			
    			if(i!=1)
    			str=str.substring(index+1,str.length());
    			index=str.indexOf(s2.charAt(i));
    		
    			
    			
    			if(index==-1)
    				return false;
    		
    			
    		}
    	}
    	return true;
    
    }
    

    }


Log in to reply
 

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