-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep1.java
More file actions
50 lines (47 loc) · 918 Bytes
/
step1.java
File metadata and controls
50 lines (47 loc) · 918 Bytes
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
42
43
44
45
46
47
48
49
50
/* "aabbbbcccd"
“”
“a”
“aadddd”
“aaaadd” */
// returns longest substring of repeated characters in str
string solve(string str){
string res=””;
int N=str.length();
for(int i=0;i!=N;i++){
char c=str.charAt(i);
int j=i+1;
while(j!=N && str.chatAt(j) == c)){
j++;
}
if(j-i > res.length()) res=str.substring(i,j);
i=j-1;
}
return res;
}
/* 012 → 020
222 1000
0
1
2
20
21
212
3
-1
-10 → -2 */
// Takes a string of base 3, returns that string + 1 in base 3
string solve2 (string str){
string res= ””;
if(str.length()==0) //throw an exception
int i=str.length()-1;
while(i!=-1 && str.charAt(i) == ‘2’){
res=’0’+res;
i--;
}
if(i==-1) res=’1’+res;
else{
res= (char)(str.chatAt(i)+1)+res;
res= res.substring(0,i)+res;
}
return res;
}