[Interview ]1. Arrays and Strings

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

  1. first check if the string is an ASCII string or a Unicode string.
  2. 如果字符串長度超過字母表中所有字母的個數,則一定存在重復字符。
    方法一:
    使用boolean數組表示每個字符是否出現。例如如果字符串是ASCII則可以初始化一個長度為256的字符數組表示每個字符是否出現。-> 時間復雜度O(n), 空間復雜度O(1)

方法二:
使用bit vector表示一個字符是否出現,每一位表示一個字符。

如果不能使用額外的數據結構:

  1. 使用每個字符以此與其他字符進行比較 -> 時間復雜度O(n^2)
  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

  1. 如果兩個字符串具有完全相同的字母組合則認為這兩個字符串互為排列,因此,可以計算每個字符串里面字符出現的個數進行比較,字符串計數的方法通過一個int數組決定
  2. 可以先對兩個字符串進行排列,排列后逐個字母進行對比

Solution
需要預先了解大小寫空格是否對結果有影響

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
首先統計字符串中有多少個空格,由此推算將空格替換為%20后字符串有多長。如果原來字符串長度為len,其中有c個空格,那么處理后的字符串長度為len + 2c;因此我們可以得到擴展之后最后一個字符應該出現的位置。
對字符進行urlify的過程將從字符串的結尾向前進行。

1.4 Palindrome Permutation -> todo 實現優化算法的代碼

EXAMPLE
Input: Tact Coa
Output: True (permutations: "taco cat'; "atco eta·; etc.)

Idea
對字符串中的數組進行計數,如果最多只有一個字符出現的次數為計數則認為這個字符串可以通過排列得到一個回文字符串。忽略其中的空白字符串

Solution
優化:使用bit vector表示一個字符出現次數的奇偶性

1.5 One Away -> todo 實現代碼

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則肯定不符合條件。
逐個判斷兩個字符串是否相等,如果不相等,則可以通過以下方式進行修改 => 首先假設字符串為A和B,當前比較的index為 i 和 j

  1. 如果A(i) 不等于 B(j),但是A(i) = B(j+1)則可以通過向A中填充B(j)實現修改,i不變,j=j+1繼續比較
  2. 如果A(i+1) = B(j+1),則可以通過修改A(i) = B(j)達到目的
  3. 如果A(i+1) = B(j)則可以刪除A(i),那么i = i +1 繼續比較

在出現一次edit后將一個flag變量設置為true,如果再次需要修改則返回false

Solution
長度差異決定需要修改的方式!!!

1.6 String Compression -> 實現代碼

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
注意:如果壓縮之后字符串的長度不小于原字符串的長度則放棄壓縮。

  1. 首先判斷一個壓縮后的字符串長度與原字符串長度的大小
  2. 如果需要則壓縮字符串,使用一個int數組記錄每個字符出現的個數

1.7 Rotate Matrix -> todo 實現代碼

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 實現代碼

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.
首先遍歷整個數組,如果一個元素為0,則將它所在的第一行和第一列對應的位置設置為0。
再次遍歷數組,根據第一行和第一列的標志信息將對應的行和列設置為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").

解法厲害!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。