-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathAlternateCapitalization.java
31 lines (24 loc) · 1 KB
/
AlternateCapitalization.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
Given a string, capitalize the letters that occupy even indexes and odd indexes separately, and return as shown below. Index 0 will be considered even.
For example, capitalize("abcdef") = ['AbCdEf', 'aBcDeF']. See test cases for more examples.
The input will be a lowercase string with no spaces.
*/
public class AlternateCapitalization {
public static String[] capitalize(String s) {
StringBuilder even = new StringBuilder();
StringBuilder odd = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (i % 2 == 0) {
even.append(Character.toUpperCase(s.charAt(i)));
odd.append(Character.toLowerCase(s.charAt(i)));
} else {
odd.append(Character.toUpperCase(s.charAt(i)));
even.append(Character.toLowerCase(s.charAt(i)));
}
}
String[] result = new String[2];
result[0] = even.toString();
result[1] = odd.toString();
return result;
}
}