Skip to content

Instantly share code, notes, and snippets.

/*
Implement an algorithm to determine if a string has all unique characters what if you can't use additional data structure
Date: 08/26/2014
*/
class solution{
// Time complexity O(n^2), Space complexity O(1)
public boolean check( String s ){
boolean result = true;
if( s.length() < 2 )// include string is empty
result = true;
class unique{
public boolean check(String s){
if(s.length == 0)
return true;
String sorted = Sort(s);
for(int i = 0; i < sorted.length - 1 ; i ++){
if ( sorted.charAt( i ) == sorted.charAt( i + 1 ))
return false;
}
/**Implement a function void reverse(char* str) in C or C++ which reverses a null-terminated string.
Time complexity O(n), space complexity O(1)
Date: 08/27/2014
*/
void swap(char* a, char* b){
char c = *a;
*a = *b;
*b = c;
}
/*1.3 Given two strings, write a method to decide if one is a permutation of the other.
space complexity:
time complexity:
date: 09/01/2014
*/
public class permutationString{
public static boolean permutation(String s1, String s2){
if(s1 ==NULL || s2 == NULL)
return false;
if(s1.isEmpty()&& s2.isEmpty())
/*1.4 Write a method to replace all spaces in a string with '%20'. You may assume that the string has sufficient space at the end of the string to hold the additional characters, and that you are given the "true" length of the string. (Note: if implementing in Java, please use a character array so that you can perform this operation in place.)
EXAMPLE
Input: "Mr John Smith "
Output: "Mr%20John%20Smith"
Time complexity: O(n)
Space complexity: O(1)
Date: 09/02/2014*/
public class replaceSpace{
public void replace(char[] str){
int len = strlen(str);
/*implement a method to perform basic string compression using the
counts of repeated characters. For example, the string aabcccccaaa would
become a2b1c5a3. If the "compressed" string would not become smaller than
the original string, your method should return the original string.
Time complexity: O(n)
Space complexity: O(n)
Date: 09/02/2014
*/
public class compressed{
@XinboChen
XinboChen / CC1.7.java
Created September 14, 2014 16:01
Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.
public static int[][] setMatrixzero(int[][] matrix){
int M = matrix.length;
int N = matrix[0].length;
boolean setRowZero[] = new boolean [M];
boolean setColumnZero[] = new boolean[N];
for(int i = 0; i < M; i++){
for(int j=0; j<N; j++)
if(matrix[i][j] ==0){
setRowZero[i]=0;
@XinboChen
XinboChen / cc1.8.java
Created September 14, 2014 21:24
Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring (e.g.,"waterbottle"is a rotation of"erbottlewat").
/*
Date: 09/14/2014
Time Complexity:
Space Complexity:
*/
public class solution{
public static boolean isSubString(String s1, String s2){
if(s1.contain(s2))
return true;
class deleteDup{
public void deleteDup(LinkNode* head){
if(head == NULL)
return;
LinkNode* curr = head;
while(curr!=NULL){
LinkNode runner = curr;
while(runner.next!=NULL){
if(runner.next.value == curr.value){
runner.next = runner.next.next;
/*
2.2 Implement an algorithm to find the kth to last element of a singly linked list.
*/
public class findElement{
public int findElement(LinkNode* head,int k){
if(head == NULL)
return;
LinkNode* curr = head;
LinkNode* kStep = head;