-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringAndSequence.java
More file actions
executable file
·77 lines (67 loc) · 2.05 KB
/
Copy pathStringAndSequence.java
File metadata and controls
executable file
·77 lines (67 loc) · 2.05 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
72
73
74
75
76
77
import java.util.*;
public class StringAndSequence {
public static String removeDuplicates(String text) {
String result = "";
for (int i = 0; i < text.length(); i++) {
if (i == 0 || text.charAt(i) != result.charAt(result.length() - 1)) {
result += text.charAt(i);
}
}
return result;
}
public static String uniqueCharacters (String text) {
LinkedHashSet<Character> hash = new LinkedHashSet<>();
String uniqueChars = "";
for(int i = 0 ; i < text.length() ; i++)
hash.add(text.charAt(i));
for(Character ch : hash)
uniqueChars += ch;
return uniqueChars;
}
public static int countSafeSquaresRooks(int n, boolean[][] rooks) {
int[] rows = new int[n];
int[] cols = new int[n];
int safeRows = 0;
int safeColumns = 0;
for (int i = 0; i < n; i++) {
rows[i] = 0;
cols[i] = 0;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (rooks[i][j] == true) {
rows[i] = 1;
cols[j] = 1;
}
}
}
for (int i = 0; i < n; i++) {
if (rows[i] == 0) {
safeRows++;
}
if (cols[i] == 0) {
safeColumns++;
}
}
return safeRows * safeColumns;
}
public static int recaman(int n){
int[] arr = new int[n+1];
boolean[] complete = new boolean[10 * n];
arr[0] = 0;
complete[0] = true;
for (int i = 1; i <= n; i++){
int neg = arr[i - 1] - i;
int pos = arr[i - 1] + i;
if ((neg > 0) && (!complete[neg])){
arr[i] = neg;
complete[neg] = true;
}
else{
arr[i] = pos;
complete[pos] = true;
}
}
return arr[n];
}
}