Skip to content

Commit 5e134b4

Browse files
authored
Added Check Algorithm Program
1 parent 34a464b commit 5e134b4

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

Check Algorithm.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// https://www.facebook.com/abhi.sensharma/posts/1241788872853419
2+
// Subscribed by Abhishek Sharma
3+
4+
// Here I have added the Check Algorithm program using Java
5+
6+
import java.util.Scanner;
7+
8+
public class Main {
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
12+
int T = sc.nextInt();
13+
for (int tc = 0; tc < T; ++tc) {
14+
String S = sc.next();
15+
16+
System.out.println(solve(S) ? "YES" : "NO");
17+
}
18+
19+
sc.close();
20+
}
21+
22+
static boolean solve(String S) {
23+
StringBuilder compressed = new StringBuilder();
24+
char current = 0;
25+
int count = -1;
26+
for (int i = 0; i <= S.length(); ++i) {
27+
if (i != S.length() && S.charAt(i) == current) {
28+
count++;
29+
} else {
30+
if (count > 0) {
31+
compressed.append(current).append(count);
32+
}
33+
34+
if (i != S.length()) {
35+
current = S.charAt(i);
36+
count = 1;
37+
}
38+
}
39+
}
40+
41+
return compressed.length() < S.length();
42+
}
43+
}

0 commit comments

Comments
 (0)