Skip to content

Latest commit

 

History

History
172 lines (165 loc) · 9.72 KB

File metadata and controls

172 lines (165 loc) · 9.72 KB

String

Concept

  • Substring
    • A substring is a contiguous non-empty sequence of characters within a string.
  • Subsequence
    • A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
  • Anagrams
    • An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
  • Shifting
    • Shifting a character forward means replacing it with the next letter in the alphabet (wrapping around so that 'z' becomes 'a').
    • Shifting a character backward means replacing it with the previous letter in the alphabet (wrapping around so that 'a' becomes 'z').

Strategies

  • Parentheses
    • Possible solutions for solving parentheses problems:
      • Stack
        • Basic logic
          • Meet (, push the index into stack.
          • Meet ), pop the index of the corresponding ( from stack.
        • Template
          Stack<Integer> stack = new Stack<>();
          for (int i = 0; i < s.length(); i++) {
              if (s.charAt(i) == '(') {               // meet (
                  stack.push(i);
              } else if (s.charAt(i) == ')') {        // meet )
                  if (!stk.isEmpty()) {
                      int leftIndex = stack.pop();
                  } else {                            // if there is no corresponding (
                  }
              } else {                                // meet other characters except parenthese
                  
              }
          }
        • Notes
          • Push the index rather than pushing the actual parenthese into the stack.
      • Counter
        • Basic logic
          • Meet (, counter increased by 1;
          • Meet ), counter decreased by 0;
  • Group string array
    • Find a encoding logic, which 2 strings should be grouped together will have the same encoding value.
  • Palindrome
    • Possible solutions for solving parentheses problems:
      • Stack
      • 2 Pointers (Meet pointers)
        • Template
          boolean isPalindrome(String s, int left, int right) {
              while (left < right) {
                  if (s.charAt(left) != s.charAt(right)) {
                      return false;
                  }
                  left++;
                  right--;
              }
              return true;
          }
    • Note
      • Need to consider 2 possible cases: length of palindrome is even or odd.

Common topics

back to Problem_Categories

back to Problem_Categories