Java char array solution, 13ms beats 98%


  • 0
    Z

    The idea is as simple as other solutions: append the upper case of the character in the original string if it is not '-', and add '-' every K characters. But using char array here may be faster than string builder.

    	public String licenseKeyFormatting(String S, int K) {
    		int count = 0;
    		String[] str = S.split("-");
    		for (String s : str) {
    			count += s.length();
    		}
    		char[] ch = S.toCharArray();
    		char[] r = new char[count + (count - 1) / K];
    
    		int ch_i = ch.length - 1, r_i = r.length - 1;
    		int r_count = 1;
    		while (ch_i >= 0 && r_i >= 0) {
    			if (ch[ch_i] == '-') {
    				ch_i--;
    				continue;
    			}
    			if (r_count % (K + 1) == 0) {
    				r[r_i] = '-';
    			} else {
    				r[r_i] = Character.toUpperCase(ch[ch_i]);
    				ch_i--;
    			}
    			r_i--;
    			r_count++;
    		}
    		return new String(r);
    	}
    

Log in to reply
 

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