-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpperCase.java
More file actions
41 lines (38 loc) · 1.32 KB
/
UpperCase.java
File metadata and controls
41 lines (38 loc) · 1.32 KB
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
32
33
34
35
36
37
38
39
40
41
public class UpperCase {
public static String touppercase(String str) { //O(n) -- Time Complixity
StringBuilder sb = new StringBuilder("");
char ch = Character.toUpperCase(str.charAt(0));
sb.append(ch);
for(int i = 1; i<str.length();i++) {
if (str.charAt(i) == ' ' && i<str.length()-1) {
sb.append(str.charAt(i));
i++;
sb.append(Character.toUpperCase(str.charAt(i)));
}else {
sb.append(str.charAt(i));
}
}
return sb.toString();
}
public static String compres(String str) { //O(n) -- Time Complixity
String newstr ="";
for(int i =0;i<str.length();i++){
Integer count =1;
while (i<str.length()-1 && str.charAt(i) == str.charAt(i+1)) {
count++;
i++;
}
newstr += str.charAt(i);
if (count>1) {
newstr += count.toString();
}
}
return newstr;
}
public static void main(String[] args) {
// String str = "hi, i am utkarsh";
// System.out.println(touppercase(str));
String str = "aaabbcccdd";
System.out.println(compres(str));
}
}