Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
867 B
1
Indexable
Never
// it have TLE
public static String solveOld(String A) {
		String R = "";

		for (int i = 0; i < A.length(); i++) {
			char c = A.charAt(i);
			if (c >= 'a' && c <= 'z') {
				R += Character.toUpperCase(c);
			} else {
				R += Character.toLowerCase(c);
			}
		}

		return R;
	}

// It worked
	public static String solve(String A) {
		StringBuilder R = new StringBuilder();

		for (int i = 0; i < A.length(); i++) {
			char c = A.charAt(i);
			if (c >= 'a' && c <= 'z') {
				R.append(Character.toUpperCase(c));
			} else {
				R.append(Character.toLowerCase(c));
			}
		}

		return R.toString();
	}


String is slow and consumes more memory when we concatenate too many strings because every time it creates new instance.	
StringBuffer is fast and consumes less memory when we concatenate t strings.

Can you please explain how?