Hash Tables
ArrayList & Resizeable Arrays
amortized insertion running time is O(1)
StringBuilder
The time complexity for using "+" to concatenate a string array is O(x + 2x + 3x + ... + nx) = O(xn^2).
String joinWords(String[] words) {
String sentence = "";
for (String w : words) {
sentence = sentence + w;
}
return sentence;
}
StringBuidler helps avoid the problem by using a resizable array of all the strings.
Exercise Method
Impelement your own version of StringBuilder, HashTable and ArrayList.
Additional Reading
- Hash Table Collision Resolution (pg 636)
- Rabin-Karp Substring Search (pg636)
Interview Question
1.1 Is Unique
Implement an algorithm to determine if a string has all unique characters. What if you cannot use an additional data structure?
Idea: use a hash set to put all seen chars, check each char with the hash set to see if it has been seen before. -- This uses an additional data structure, a hash set.
Solution
- first check if the string is an ASCII string or a Unicode string.
- 如果字符串長度超過字母表中所有字母的個數(shù),則一定存在重復字符。
方法一:
使用boolean數(shù)組表示每個字符是否出現(xiàn)。例如如果字符串是ASCII則可以初始化一個長度為256的字符數(shù)組表示每個字符是否出現(xiàn)。-> 時間復雜度O(n), 空間復雜度O(1)
方法二:
使用bit vector表示一個字符是否出現(xiàn),每一位表示一個字符。
如果不能使用額外的數(shù)據(jù)結(jié)構(gòu):
- 使用每個字符以此與其他字符進行比較 -> 時間復雜度O(n^2)
- 如果可以修改字符串,則可以先排序,再比較相鄰字符,時間復雜度O(n * logn)
1.2 Check Permutation
Given two strings, write a method to decide if one is a permutation of the other
**Idea
- 如果兩個字符串具有完全相同的字母組合則認為這兩個字符串互為排列,因此,可以計算每個字符串里面字符出現(xiàn)的個數(shù)進行比較,字符串計數(shù)的方法通過一個int數(shù)組決定
- 可以先對兩個字符串進行排列,排列后逐個字母進行對比
Solution
需要預(yù)先了解大小寫和空格是否對結(jié)果有影響
1.3 URLify
URLify: Write a method to replace all spaces in a string with '%20'. You may assume that the string has sufficient space at the end 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 ", 13
Output: "Mr%20John%20Smith"
Idea
首先統(tǒng)計字符串中有多少個空格,由此推算將空格替換為%20后字符串有多長。如果原來字符串長度為len,其中有c個空格,那么處理后的字符串長度為len + 2c;因此我們可以得到擴展之后最后一個字符應(yīng)該出現(xiàn)的位置。
對字符進行urlify的過程將從字符串的結(jié)尾向前進行。
1.4 Palindrome Permutation -> todo 實現(xiàn)優(yōu)化算法的代碼
EXAMPLE
Input: Tact Coa
Output: True (permutations: "taco cat'; "atco eta·; etc.)
Idea
對字符串中的數(shù)組進行計數(shù),如果最多只有一個字符出現(xiàn)的次數(shù)為計數(shù)則認為這個字符串可以通過排列得到一個回文字符串。忽略其中的空白字符串
Solution
優(yōu)化:使用bit vector表示一個字符出現(xiàn)次數(shù)的奇偶性
1.5 One Away -> todo 實現(xiàn)代碼
There are three types of edits that can be performed on strings: insert a character, remove a character, or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away.
EXAMPLE
pale, ple -> true
pales, pale -> true
pale, bale -> true
pale, bae -> false
Idea:
因為判斷只需要修改以此,那么首先判斷兩個字符串的長度差別是不是1,如果大于1則肯定不符合條件。
逐個判斷兩個字符串是否相等,如果不相等,則可以通過以下方式進行修改 => 首先假設(shè)字符串為A和B,當前比較的index為 i 和 j
- 如果A(i) 不等于 B(j),但是A(i) = B(j+1)則可以通過向A中填充B(j)實現(xiàn)修改,i不變,j=j+1繼續(xù)比較
- 如果A(i+1) = B(j+1),則可以通過修改A(i) = B(j)達到目的
- 如果A(i+1) = B(j)則可以刪除A(i),那么i = i +1 繼續(xù)比較
在出現(xiàn)一次edit后將一個flag變量設(shè)置為true,如果再次需要修改則返回false
Solution
長度差異決定需要修改的方式!!!
1.6 String Compression -> 實現(xiàn)代碼
Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your method should return the original string. You can assume the string has only uppercase and lowercase letters (a - z).
Idea
注意:如果壓縮之后字符串的長度不小于原字符串的長度則放棄壓縮。
- 首先判斷一個壓縮后的字符串長度與原字符串長度的大小
- 如果需要則壓縮字符串,使用一個int數(shù)組記錄每個字符出現(xiàn)的個數(shù)
1.7 Rotate Matrix -> todo 實現(xiàn)代碼
Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?
1.8 Zero Matrix -> todo 實現(xiàn)代碼
Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.
Idea
使用第一行和第一列作為標志位,并使用isColZero和isRowZero兩個標志表示第一列和第一行本身是否為0.
首先遍歷整個數(shù)組,如果一個元素為0,則將它所在的第一行和第一列對應(yīng)的位置設(shè)置為0。
再次遍歷數(shù)組,根據(jù)第一行和第一列的標志信息將對應(yīng)的行和列設(shè)置為0
1.9 String Rotation
Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, 51 and 52, write code to check if 52 is a rotation of 51 using only one call to isSubstring (e.g., "waterbottle" is a rotation of" erbottlewat").
解法厲害!