My Java Solution


  • 0
    G

    ...
    public class Solution {
    public String fractionToDecimal(int numerator, int denominator) {

    	  String valueToSend = null;
    	  String valueReceived=null;
    	  double result = 0;
    	  String returnResult = null;
    		result = numerator/denominator;
    	
       	  	int indexOfDot = -1;
       	  	indexOfDot = String.valueOf(result).indexOf(".");
       	  	
       	    valueToSend = String.valueOf(result).substring(indexOfDot+1);
    		
       	    valueReceived = getString(valueToSend);
       	     
       	     if(valueReceived.equalsIgnoreCase("0")) {
       	    	returnResult = String.valueOf(result).substring(0,indexOfDot); 
       	     }
       	     else {
       	    	 String firstPart =String.valueOf(result).substring(0,indexOfDot+1); 
       	    	returnResult = firstPart+valueReceived;
       	     }
         
         return returnResult;
    	   
                
    }
    
    public String getString(String input) {
    	String firstString=null;
    	int length=0;
    	String repeatValue = null;
    	Set <String> repeatPattern = new HashSet<String>();
    	boolean doesPatternExist = false;
    	String firstPart = null;
    	String lastPart = null;
    	String retValue = null;
    	int lastIndex = -1;
    	for(int i=1;i<input.length();i++) {
    		firstString = input.substring(0,i);
    		length = i;
    		doesPatternExist = doesPatternExist(length, input,repeatPattern);
    		System.out.println(doesPatternExist);
    		if(doesPatternExist) {
    			Iterator it = repeatPattern.iterator();
    			while(it.hasNext()) {
    				repeatValue = (String)it.next();
    			}
    			System.out.println("Repeatchar is"+repeatValue);
    			break;
    		}			
    	}
    	
    	if(doesPatternExist) {
    		lastIndex = input.lastIndexOf(repeatValue)+repeatValue.length();
    		lastPart = input.substring(lastIndex);
    		firstPart = "("+repeatValue+")";
    		retValue = firstPart+lastPart;
    	}
    	else {
    		retValue = input;
    	}
    
    	System.out.println("Calculated Expression is "+retValue);
    	return retValue;
    }
    
    
    public boolean doesPatternExist(int length,String input,Set<String> repeatedString) {
    	String firstString = input.substring(0, length);
    	boolean endReached = false;
    	String nextString = null;
    	int nextIndex = 0;
    	while(!endReached) {
    		nextIndex = nextIndex+length;
    		if(nextIndex+length<=input.length()) {
    			nextString = input.substring(nextIndex,nextIndex+length);
    		}
    		else {
    			endReached=true;
    			repeatedString.add(nextString);
    			
    		}
    		
    		if(nextString!=null) {				
    			 if(!nextString.equalsIgnoreCase(firstString)) {
    				return false;					 
    			 }
    			 else {
    				 repeatedString.add(nextString);
    				return true;					 
    			 }
    		}
    		
    		if(nextIndex>input.length()) {
    			endReached=true;
    			repeatedString.add(nextString);
    			break;
    		}
    		
    	}
    	if(nextString!=null && nextString.equalsIgnoreCase(firstString)) {
    		repeatedString.add(nextString);
    		return true;
    	}
    	else {
    		return false;
    	}
    }
    

    }

    ...


Log in to reply
 

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