FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/StringOps/src/StringOps.java at master · wodgithub/algorithm · GitHub
wodgithub
algorithm
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
StringOps
/
src
/
StringOps.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
339 lines (277 loc) · 9.82 KB
Breadcrumbs
algorithm
/
StringOps
/
src
/
StringOps.java
Copy path
File metadata and controls
339 lines (277 loc) · 9.82 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Arrays
;
import
java
.
util
.
HashMap
;
public
class
StringOps
{
public
static
void
main
(
String
[]
args
) {
String
s1
=
"aaadbscddddedee"
;
System
.
out
.
println
(
"s1: "
+
s1
);
String
lrs
=
LRS
(
s1
);
System
.
out
.
println
(
"LRS: "
+
lrs
);
String
lrs1
=
LRS1
(
s1
);
System
.
out
.
println
(
"LRS1: "
+
lrs1
);
String
s2
=
"ddaacddddess"
;
System
.
out
.
println
(
"s2: "
+
s2
);
String
lcs
=
LCS
(
s1
,
s2
);
System
.
out
.
println
(
"LCS: "
+
lcs
);
String
lcs1
=
LCS1
(
s1
,
s2
);
System
.
out
.
println
(
"LCS1: "
+
lcs1
);
String
s3
=
"abcdefgh"
;
System
.
out
.
println
(
"s3: "
+
s3
);
int
k
=
3
;
String
rs3
=
rotateString
(
s3
,
k
);
System
.
out
.
println
(
"rotate "
+
s3
+
" in "
+
k
+
" : "
+
rs3
);
String
s4
=
"abc"
;
String
s5
=
"a*b."
;
System
.
out
.
println
(
s4
+
" : "
+
s5
+
" : "
+
isRexMatch
(
s4
,
s5
));
s5
=
"a*b*.*"
;
System
.
out
.
println
(
s4
+
" : "
+
s5
+
" full lenght match : "
+
isRexMatch1
(
s4
,
s5
));
System
.
out
.
println
(
"perm s4: "
+
s4
);
perm
(
""
,
s4
);
System
.
out
.
println
(
"perm1 s4: "
+
s4
);
perm1
(
s4
.
toCharArray
(),
s4
.
length
());
String
s6
=
"abcd"
;
System
.
out
.
println
(
"move s6: "
+
s6
);
move
(
s6
.
toCharArray
(),
s6
.
length
(),
new
ArrayList
<
String
>(),
true
);
HashMap
<
Integer
,
String
>
phoneDict
=
new
HashMap
<
Integer
,
String
>();
phoneDict
.
put
(
2
,
"abc"
);
phoneDict
.
put
(
3
,
"def"
);
phoneDict
.
put
(
4
,
"ghi"
);
phoneDict
.
put
(
5
,
"gkl"
);
phoneDict
.
put
(
6
,
"mno"
);
phoneDict
.
put
(
7
,
"pqrs"
);
phoneDict
.
put
(
8
,
"tuv"
);
phoneDict
.
put
(
9
,
"wxyz"
);
System
.
out
.
println
(
"Generate words from phone pad"
);
generateWordsFromPhone
(
"24"
,
""
,
phoneDict
);
}
public
static
void
generateWordsFromPhone
(
String
digits
,
String
prefix
,
HashMap
<
Integer
,
String
>
phoneDict
) {
if
(
digits
.
length
() ==
0
) {
System
.
out
.
println
(
prefix
);
return
;
}
String
dict
=
phoneDict
.
get
(
Integer
.
valueOf
(
digits
.
substring
(
0
,
1
)));
String
rest
=
digits
.
substring
(
1
);
for
(
int
i
=
0
;
i
<
dict
.
length
();
i
++) {
generateWordsFromPhone
(
rest
,
prefix
+
dict
.
substring
(
i
,
i
+
1
),
phoneDict
);
}
}
// longest repeated string
public
static
String
LRS
(
String
s
) {
StringBuilder
sb1
=
new
StringBuilder
();
String
sb2
=
""
;
// it is a holder for LRS
char
[]
chars
=
s
.
toCharArray
();
sb1
.
append
(
chars
[
0
]);
for
(
int
i
=
1
;
i
<
chars
.
length
;
i
++) {
if
(
chars
[
i
-
1
] ==
chars
[
i
]) {
sb1
.
append
(
chars
[
i
]);
}
else
{
if
(
sb1
.
length
() >
sb2
.
length
()) {
sb2
=
sb1
.
toString
();
}
sb1
=
new
StringBuilder
();
sb1
.
append
(
chars
[
i
]);
}
}
return
sb2
;
}
public
static
String
LRS1
(
String
st
) {
char
[]
chars
=
st
.
toCharArray
();
int
i
=
0
,
s
=
0
,
e
=
0
,
j
=
i
;
while
(
i
<
chars
.
length
) {
if
(
j
-
i
>=
e
-
s
) {
s
=
i
;
e
=
j
;
}
i
=
j
;
for
(
j
=
i
;
j
<
chars
.
length
;
j
++) {
if
(
chars
[
i
] !=
chars
[
j
])
break
;
}
}
char
[]
lrs
=
Arrays
.
copyOfRange
(
chars
,
s
,
e
);
return
new
String
(
lrs
);
}
// longest common prefix
public
static
String
LCP
(
String
s1
,
String
s2
) {
int
n
=
Math
.
min
(
s1
.
length
(),
s2
.
length
());
for
(
int
i
=
0
;
i
<
n
;
i
++) {
if
(
s1
.
charAt
(
i
) !=
s2
.
charAt
(
i
))
return
s1
.
substring
(
0
,
i
);
}
return
s1
;
}
// longest common string
public
static
String
LCS
(
String
x
,
String
y
) {
int
N
=
x
.
length
();
int
M
=
y
.
length
();
String
[]
suffixes
=
new
String
[
M
+
N
];
for
(
int
i
=
0
;
i
<
N
;
i
++) {
suffixes
[
i
] =
x
.
substring
(
i
);
}
for
(
int
i
=
0
;
i
<
M
;
i
++) {
suffixes
[
N
+
i
] =
y
.
substring
(
i
);
}
Arrays
.
sort
(
suffixes
);
String
lrs
=
""
;
for
(
int
i
=
0
;
i
<
suffixes
.
length
-
1
;
i
++) {
String
lcps
=
LCP
(
suffixes
[
i
],
suffixes
[
i
+
1
]);
if
(
lcps
.
length
() >
lrs
.
length
())
lrs
=
lcps
;
}
return
lrs
;
}
//back-track
public
static
String
LCS1
(
String
x
,
String
y
) {
int
[][]
lcs
=
new
int
[
x
.
length
()][
y
.
length
()];
// all 0
int
max
=
0
;
int
end
=
0
;
for
(
int
i
=
0
;
i
<
x
.
length
();
i
++) {
for
(
int
j
=
0
;
j
<
y
.
length
();
j
++) {
if
(
x
.
charAt
(
i
) ==
y
.
charAt
(
j
)) {
if
(
i
==
0
||
j
==
0
)
lcs
[
i
][
j
] =
1
;
else
lcs
[
i
][
j
] =
lcs
[
i
-
1
][
j
-
1
] +
1
;
if
(
lcs
[
i
][
j
] >
max
) {
max
=
lcs
[
i
][
j
];
end
=
i
;
}
}
}
}
return
x
.
substring
(
end
-
max
+
1
,
end
+
1
);
}
public
static
String
reverse
(
String
st
,
int
s
,
int
e
) {
if
(
s
<
0
||
e
>
st
.
length
())
return
st
;
char
[]
chars
=
st
.
toCharArray
();
while
(
s
<
e
) {
char
tmp
=
chars
[
s
];
chars
[
s
] =
chars
[
e
];
chars
[
e
] =
tmp
;
s
++;
e
--;
}
return
new
String
(
chars
);
}
public
static
String
rotateString
(
String
s
,
int
k
) {
int
n
=
s
.
length
();
/*
String rs1 = reverse(s, 0, n - 1);
String rs2 = reverse(rs1, 0, n-k-1);
String rs3 = reverse(rs2, n-k, n - 1);
*/
String
rs1
=
reverse
(
s
,
0
,
k
-
1
);
String
rs2
=
reverse
(
rs1
,
k
,
n
-
1
);
String
rs3
=
reverse
(
rs2
,
0
,
n
-
1
);
return
rs3
;
}
public
static
boolean
isRexMatch
(
String
s1
,
String
s2
) {
if
(
s1
==
null
||
s2
==
null
)
return
false
;
if
(
s2
.
isEmpty
())
return
s2
.
isEmpty
();
// "" , ""
if
(
s2
.
length
() ==
1
) {
if
(
s2
.
charAt
(
0
) ==
s1
.
charAt
(
0
) ||
s2
.
charAt
(
0
) ==
'.'
||
s2
.
charAt
(
0
) ==
'*'
) {
return
true
;
}
else
return
false
;
}
if
(
s2
.
length
() >=
2
) {
if
(
s2
.
charAt
(
1
) !=
'*'
) {
if
(
s1
.
charAt
(
0
) ==
s2
.
charAt
(
0
) ||
s2
.
charAt
(
0
) ==
'.'
) {
if
(
s1
.
length
() >
1
)
return
isRexMatch
(
s1
.
substring
(
1
),
s2
.
substring
(
1
));
else
return
true
;
}
else
return
false
;
}
else
{
// s2.charAt(1) == '*'
if
((
s2
.
charAt
(
0
) ==
'.'
) || (
s1
.
charAt
(
0
) ==
s2
.
charAt
(
0
))) {
if
(
s1
.
length
() ==
1
)
return
true
;
else
if
(
s2
.
length
() >
2
)
return
isRexMatch
(
s1
,
s2
.
substring
(
2
)) ||
isRexMatch
(
s1
.
substring
(
1
),
s2
.
substring
(
2
));
else
//s2.length() == 2
return
true
;
}
else
return
false
;
}
}
return
isRexMatch
(
s1
.
substring
(
1
),
s2
.
substring
(
2
));
}
public
static
boolean
isRexMatch1
(
String
s1
,
String
s2
) {
if
(
s1
==
null
||
s2
==
null
)
return
false
;
if
(
s2
.
isEmpty
())
return
s2
.
isEmpty
();
// "" , ""
if
(
s2
.
length
() ==
1
) {
if
((
s2
.
charAt
(
0
) ==
s1
.
charAt
(
0
) ||
s2
.
charAt
(
0
) ==
'.'
) && (
s1
.
length
() ==
1
)) {
return
true
;
}
else
return
false
;
}
if
(
s2
.
length
() >=
2
) {
if
(
s2
.
charAt
(
1
) !=
'*'
) {
if
(
s1
.
charAt
(
0
) ==
s2
.
charAt
(
0
) ||
s2
.
charAt
(
0
) ==
'.'
) {
if
(
s1
.
length
() >
1
)
return
isRexMatch1
(
s1
.
substring
(
1
),
s2
.
substring
(
1
));
else
return
false
;
}
else
return
false
;
}
else
{
// s2.charAt(1) == '*'
if
((
s2
.
charAt
(
0
) ==
'.'
) || (
s1
.
charAt
(
0
) ==
s2
.
charAt
(
0
))) {
if
(
s1
.
length
() ==
1
)
if
(
s2
.
length
() ==
2
)
return
true
;
else
return
false
;
else
// (s1.length() > 1)
if
(
s2
.
length
() >
2
)
return
isRexMatch1
(
s1
,
s2
.
substring
(
2
)) ||
isRexMatch1
(
s1
.
substring
(
1
),
s2
.
substring
(
2
));
else
// s2.length() == 2
return
isRexMatch1
(
s1
.
substring
(
1
),
s2
);
}
else
return
false
;
}
}
return
isRexMatch1
(
s1
.
substring
(
1
),
s2
.
substring
(
2
));
}
public
static
void
perm
(
String
prefix
,
String
s
) {
int
N
=
s
.
length
();
if
(
N
==
0
) {
System
.
out
.
println
(
prefix
);
return
;
}
for
(
int
i
=
0
;
i
<
N
;
i
++)
perm
(
prefix
+
s
.
charAt
(
i
),
s
.
substring
(
0
,
i
) +
s
.
substring
(
i
+
1
));
}
public
static
void
swap
(
char
[]
a
,
int
s
,
int
e
) {
char
tmp
=
a
[
s
];
a
[
s
] =
a
[
e
];
a
[
e
] =
tmp
;
}
// n = s.length -1
public
static
void
perm1
(
char
[]
s
,
int
n
) {
if
(
n
==
0
) {
System
.
out
.
println
(
s
);
return
;
}
for
(
int
i
=
0
;
i
<
n
;
i
++) {
swap
(
s
,
i
,
n
-
1
);
perm1
(
s
,
n
-
1
);
swap
(
s
,
i
,
n
-
1
);
}
}
// all combinations
// n = s.length
public
static
void
move
(
char
[]
s
,
int
n
,
ArrayList
<
String
>
t
,
boolean
pick
) {
if
(
n
==
0
) {
System
.
out
.
println
(
t
.
toString
());
return
;
}
move
(
s
,
n
-
1
,
t
,
true
);
if
(
pick
) {
t
.
add
(
String
.
valueOf
(
s
[
n
-
1
]));
}
else
{
t
.
remove
(
String
.
valueOf
(
s
[
n
-
1
]));
}
move
(
s
,
n
-
1
,
t
,
false
);
}
}
Back
|
FazBrowse Home
|
New Git URL