Skip to content

Instantly share code, notes, and snippets.

View MithraTalluri's full-sized avatar

Mithra Talluri MithraTalluri

View GitHub Profile
buildRectangle(title = "MyRectangle", width = 50)
fun buildRectangle(title: String, height: Int = 100, width: Int = 100) {
Rectangle(title, width, height)
}
val str1: String = null // compile time error
val str2: String? = null // compiles fine
val handler: EmailHandler? = null
handler.sendEmail() // compile time error
handler?.sendEmail() // compiles fine but doesn't call sendEmail() function
data class Person(val name: String, val age: Int)
public class Person {
private final String name;
private final int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
fun String.lastChar() = get(length - 1) //returns the last char in a string
val lastChar: Char = "abcd".lastChar() // d
val john1 = Person("John")
val john2 = Person("John")
john1 == john2 // true (structural equality)
john1 === john2 // false (referential equality)
if (obj is String) {
print(obj.length) // x is automatically cast to String
}
if (obj !is String) {
print("Not a String")
}
val i = 1 // type inferred to Int
val s = "abc" // type inferred to String
val j: Double = 1.1 // type declared explicitly
int manachersAlgorithm(String s, int N) {
String str = getModifiedString(s, N);
int len = (2 * N) + 1;
int[] P = new int[len];
int c = 0; //stores the center of the longest palindromic substring until now
int r = 0; //stores the right boundary of the longest palindromic substring until now
int maxLen = 0;
for(int i = 0; i < len; i++) {
//get mirror index of i
int mirror = (2 * c) - i;