Java solution


  • 1
    S
    public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
    		int count = 0;
    		String primary = "";
    		String secondary = "" ;
    		if(s1.length()*n1 < s2.length()*n2) {
    			return count;
    		}
    		if(s1.contains(s2)){
    			char[] primArr = s1.toCharArray();
    			char[] secArr = s2.toCharArray();
    			int j =0;
    			for(int i=0; i< primArr.length && j<secArr.length; i++) {
    				if(primArr[i] == secArr[j]) {
    					j++;
    				}
    				if(j == secArr.length) {
    					count +=1;
    					j = 0;
    				}
    			}
    			count = n1* count/n2;
    			return count;
    		}
    		if(s1 == s2) {
    			count = (n1/n2);
    			return count;
    		}
    		for(int i=1;i <= n1; i++) {
    			primary += s1;
    		}
    		for(int i=1; i<=n2; i++) {
    			secondary += s2;
    		}
    		char[] primArr = primary.toCharArray();
    		char[] secArr = secondary.toCharArray();
    		int j = 0;
    		for(int i=0; i< primArr.length && j<secArr.length; i++) {
    			if(primArr[i] == secArr[j]) {
    				j++;
    			}
    			if(j == secArr.length) {
    				count +=1;
    				j = 0;
    			}
    		}
    
    		return count;
    	}
    
    

  • 0
    K

Log in to reply
 

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