This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.lang.*; | |
import java.io.*; | |
class ShortestDistanceBetweenTwoCells { | |
static class Cell { | |
int row; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.lang.*; | |
import java.util.*; | |
class nBitGrayCode { | |
public static void main(String[] args) { | |
int n = 3; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.lang.*; | |
import java.io.*; | |
class Sakamoto { | |
static int sakamotoAlgorithm(int date, int month, int year) { | |
int[] dateDiff = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
def is_safe(x, y, matrix, visited): | |
col = len(visited[0]) | |
row = len(visited) | |
if x >= 0 and y >= 0 and x < col and y < row and visited[y][x] == False and matrix[y][x] == 1: | |
return True | |
else: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.io.*; | |
import java.lang.*; | |
public class kSwapLargestPermutation { | |
public static void main(String[] args) { | |
Integer[] numberArray = new Integer[] {4, 5, 2, 1, 3}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.math.*; | |
import java.util.*; | |
import java.lang.*; | |
public class StreamMedian { | |
public static void main(String[] args) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import math | |
def main(): | |
# take inputs | |
string = input("Enter the text to be justified:\n").split() | |
width = int(input("Enter the width of the line: ")) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def kmp(txt, pat): | |
# base conditions | |
if len(pat) == len(txt): | |
if pat == txt: | |
return 0 | |
else: | |
return -1 | |
if len(pat) > len(txt): | |
return -1 | |
if len(pat) == 0 or len(txt) == 0: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
There are 4 cases to handle | |
* Case 1 : Right side palindrome is totally contained under current palindrome. In this case do not consider this as center. | |
* Case 2 : Current palindrome is proper suffix of input. Terminate the loop in this case. No better palindrome will be found on right. | |
* Case 3 : Right side palindrome is proper suffix and its corresponding left side palindrome is proper prefix of current palindrome. Make largest such point as next center. | |
* Case 4 : Right side palindrome is proper suffix but its left corresponding palindrome is be beyond current palindrome. Do not consider this as center because it will not extend at all. | |
''' | |
def longest_palindromic_substring(string): | |
# preprocess the string to allow for even length palindromes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def longest_palindromic_substring(string): | |
length = len(string) | |
# return the string if the string is only one character long | |
if length == 1: | |
return string | |
palindrome = "" | |
palindrome_length = 0 |
NewerOlder