-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbbreiviation.java
More file actions
71 lines (64 loc) · 2.14 KB
/
Abbreiviation.java
File metadata and controls
71 lines (64 loc) · 2.14 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.HashSet;
public class Abbreiviation {
static HashSet<String> hs = new HashSet<>();
static String ss;
public static void abbRec(StringBuilder s, int i){
if(i==0) {
hs.add(s.toString());
return;
}
else {
int res = 1;
StringBuilder s1 = new StringBuilder(s);
if(i<s1.length() && !(s1.charAt(i)>=97 && s1.charAt(i)<=122)) res += (s1.charAt(i) - '0');
s1.setCharAt(i-1, (char) (res+'0'));
if(i<s1.length() && !(s1.charAt(i)>=97 && s1.charAt(i)<=122)) s1.deleteCharAt(i);
abbRec(s1, i-1);
abbRec(s, i-1);
}
}
public static void abbRec2(StringBuilder s, int i) {
if(i==ss.length()) {
hs.add(s.toString());
return;
}
else {
int res = 1;
StringBuilder s1 = new StringBuilder(s);
if(s1.length()>0 && !(s1.charAt(s1.length()-1)>=97 && s1.charAt(s1.length()-1)<=122)) {
res += (s1.charAt(s1.length()-1) - '0');
s1.setCharAt(s1.length()-1, (char) (res+'0'));
}
else s1.append(res);
abbRec2(s1, i+1);
abbRec2(s.append(ss.charAt(i)), i+1);
}
}
public static void abbRec3(StringBuilder s, int i, int k) {
if(i==ss.length()){
if(k!=0) s.append(k);
hs.add(s.toString());
return;
}
abbRec3(new StringBuilder(s), i+1, k+1);
StringBuilder s1 = new StringBuilder(s);
if(k!=0) s1.append(k);
s1.append(ss.charAt(i));
abbRec3(s1, i+1, 0);
}
public static void main(String args[]) {
String s = "pep";
ss = s;
int s1 = "123".charAt(2) - '0' + 1;
abbRec(new StringBuilder(s),s.length());
System.out.println(hs);
System.out.println(hs.size());
System.out.println((char) (s1+'0'));
hs = new HashSet<>();
abbRec2(new StringBuilder(), 0);
System.out.println(hs);
hs = new HashSet<>();
abbRec3(new StringBuilder(), 0,0);
System.out.println(hs);
}
}