Working With Strings and StringBuilder
String reversed = new StringBuilder(str).reverse().toString();
boolean isPalindrome = str.equals(new StringBuilder(str).reverse().toString());
long count = str.chars().filter(ch -> ch == <Character to Check>).count();
String[] words = str.split("\\s+");
String cleaned = str.replaceAll("[^a-zA-Z0-9]", "");
sb.deleteCharAt(index);
char[] chars = str.toCharArray();
Arrays.sort(chars);
String sorted = new String(chars);
String s = "hello world";
String sub = s.substring(0, 5);
boolean contains = s.contains("world");
String s = "abc";
for (int i = 0; i < s.length(); i++) {
for (int j = i + 1; j <= s.length(); j++) {
System.out.println(s.substring(i, j));
}
}
String s = "abcdef";
String reversed = new StringBuilder(s.substring(2, 5))
.reverse().toString();
String s = "abc";
int n = s.length();
for (int i = 0; i < (1 << n); i++) {
StringBuilder subset = new StringBuilder();
for (int j = 0; j < n; j++) {
if ((i & (1 << j)) != 0) {
subset.append(s.charAt(j));
}
}
System.out.println(subset.toString());
}
Working With Characters
if(Character.isLetter(ch)){}
if(Character.isDigit(ch)){}
int num = ch -'0';
char c = (char) (num + '0')
if("aeiouAEIOU".indexOf(ch) != -1) {}
Character.toLowerCase(ch)
Character.toUpperCase(ch)
'a' - 97
'A' - 65
'0' - 48
Working With Numbers
public int gcd(int a , int b){
return b == 0 ? a : (b, a % b);
}
public int lcm(int a , int b){
return (a * b) / gcd(a,b);
}
for (int j = 1; j * j <= n; j++) {
if (n % j == 0) {
System.out.println(j);
if (j != n / j) {
System.out.println(n / j);
}
}
}
int digits = (int) Math.log10(num) + 1;
int reverse = 0;
while(num > 0){
reversed = reversed * 10 + num % 10;
num /= 10;
}
boolean isPowerOf2 = ( n & ( n -1 )) == 0;
String str = "12345";
int number = Integer.parseInt(str);
System.out.println("Integer: " + number);
long largeNumber = Long.parseLong(str);
System.out.println("Long: " + largeNumber);
import java.math.BigInteger;
BigInteger bigNumber = new BigInteger(str);
System.out.println("BigInteger: " + bigNumber);
int[] binaryArray = {1, 0, 0, 1};
public static int binaryToDecimal(int[] binaryArray) {
int decimal = 0;
for (int i = 0; i < binaryArray.length; i++) {
decimal = decimal * 2 + binaryArray[i];
}
return decimal;
}
String binaryString = Integer.toBinaryString(decimal);
String hexString = Integer.toHexString(decimal).toUpperCase();
String octalString = Integer.toOctalString(decimal);
String binary = "1011";
int num = Integer.parseInt(binary, 2);
int result = num & 5;
System.out.println(Integer.toBinaryString(result));
Working With Bits
int setBit = n | ( 1 << position );
int unsetBit = n & ~( 1 << position );
int toggleBit = n ^ ( 1 << position );
int toggleAll = ~n
int mask = ( 1 << (bits to toggle) ) - 1;
int toggle = ~num & mask;
int count = Integer.bitCount(num);
boolean isSet = ( n & ( 1 << i) ) != 0;
Working With Arrays
Arrays.sort(arr);
int max = Arrays.stream(arr).max().getAsInt();
int min = Arrays.stream(arr).min().getAsInt();
int[] prefixSum = new int[arr.length];
prefixSum[0] = arr[0];
for(int i=1;i<arr.length;i++)
{
prefixSum[i] = prefixSum[i-1] + arr[i];
}
int sum = prefixSum[end] - ( start > 0 ? prefixSum[start -1] : 0)
boolean isSorted = IntStream.range(1, a.length).allMatch(i -> a[i] >= a[i-1])
int[] arr = {1, 2, 3};
int n = arr.length;
for (int i = 0; i < (1 << n); i++) {
List<Integer> subset = new ArrayList<>();
for (int j = 0; j < n; j++) {
if ((i & (1 << j)) != 0) {
subset.add(arr[j]);
}
}
System.out.println(subset);
}
public static void generatePermutations(int[] arr,
List<Integer> current,
boolean[] used)
{
if (current.size() == arr.length) {
System.out.println(current);
return;
}
for (int i = 0; i < arr.length; i++) {
if (!used[i]) {
used[i] = true;
current.add(arr[i]);
generatePermutations(arr, current, used);
current.remove(current.size() - 1);
used[i] = false;
}
}
}
Working With Matrix
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix[0].length; j++)
{
System.out.println(matrix[i][j]);
}
}
int[] dx = {0, 0, -1, 1};
int[] dy = {-1, 1, 0, 0};
for(int k = 0; k < 4; k++)
{
int newRow = row + dx[k];
int newCol = col + dy[k];
if(newRow >= 0 && newRow < matrix.length
&& newCol >= 0 && newCol < matrix[0].length)
{
System.out.println(matrix[newRow][newCol]);
}
}
Working With Deque
Deque<Integer> s = new ArrayDeque<>();
s.addLast(1); System.out.println(s);
s.addLast(2); System.out.println(s);
s.addLast(3); System.out.println(s);
s.removeLast(); System.out.println(s);
s.removeLast(); System.out.println(s);
Deque<Integer> q = new ArrayDeque<>();
q.addLast(1); System.out.println(q);
q.addLast(2); System.out.println(q);
q.addLast(3); System.out.println(q);
q.removeFirst(); System.out.println(q);
q.removeFirst(); System.out.println(q);
Deque<Integer> ll = new ArrayDeque<>();
ll.addLast(1); System.out.println(ll);
ll.addLast(2); System.out.println(ll);
ll.addFirst(0); System.out.println(ll);
ll.removeLast(); System.out.println(ll);
ll.removeFirst(); System.out.println(ll);