|
| 1 | +## Problems |
| 2 | + |
| 3 | +1. Valid Palindrome II |
| 4 | +2. Valid Palindrome |
| 5 | +3. Decode String |
| 6 | + |
| 7 | +## Implementation |
| 8 | + |
| 9 | +### **Valid Palindrome** |
| 10 | + |
| 11 | +***Big O:*** O(n), O(1) space |
| 12 | +``` |
| 13 | +Tips: |
| 14 | +
|
| 15 | +Two pointers. |
| 16 | +``` |
| 17 | +```c++ |
| 18 | +class Solution { |
| 19 | +public: |
| 20 | + bool isPalindrome(string s) { |
| 21 | + int st = 0; |
| 22 | + int end = s.size()-1; |
| 23 | + |
| 24 | + while (st < end) { |
| 25 | + if (!isalnum(s[st]) || !isalnum(s[end])) { |
| 26 | + while(st < end && !isalnum(s[st])) { |
| 27 | + st ++; |
| 28 | + } |
| 29 | + while(st < end && !isalnum(s[end])) { |
| 30 | + end --; |
| 31 | + } |
| 32 | + } else { |
| 33 | + if ((isalpha(s[st]) && isalpha(s[end]) && tolower(s[st]) == tolower(s[end])) || (s[st] - '0' == s[end] - '0')) { |
| 34 | + st ++; |
| 35 | + end --; |
| 36 | + } else |
| 37 | + return false; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return true; |
| 42 | + } |
| 43 | +}; |
| 44 | +``` |
| 45 | +### **Valid Palindrome II ** |
| 46 | +
|
| 47 | +***Big O:*** O(n), O(1) space |
| 48 | +``` |
| 49 | +Tips: |
| 50 | + |
| 51 | +Implement is_palindrome first. return the answer of two is_palin or together. |
| 52 | +``` |
| 53 | +```c++ |
| 54 | +class Solution { |
| 55 | +
|
| 56 | +public: |
| 57 | + // Checks if string is a palindrome |
| 58 | + bool isPalin(string &s, int start, int end) { |
| 59 | + while(start < end) { |
| 60 | + if(s[start] != s[end]) |
| 61 | + return false; |
| 62 | + ++start, --end; |
| 63 | + } |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + // TC: O(N) |
| 68 | + // SC: O(1) |
| 69 | + bool validPalindrome(string s) { |
| 70 | + for(int i = 0, j = s.size()-1; i < j; ++i, --j) { |
| 71 | + // mismatch found, only if it is the first time delete |
| 72 | + // a char and move on, else not possible |
| 73 | + if(s[i] != s[j]) { |
| 74 | + // s[0:i-1] and s[j+1, n-1] matched, |
| 75 | + // now we check if atleast s[i:j-1] or s[i+1:j] is a palindrome |
| 76 | + return (isPalin(s, i, j-1) || isPalin(s, i+1, j)); |
| 77 | + } |
| 78 | + } |
| 79 | + return true; |
| 80 | + } |
| 81 | +}; |
| 82 | +``` |
| 83 | + |
| 84 | +### **Decode String** |
| 85 | + |
| 86 | +***Big O:*** |
| 87 | + |
| 88 | +Time Complexity: O(maxK⋅n), where maxK is the maximum value of kk and nn is the length of a given string ss. We traverse a string of size nn and iterate kk times to decode each pattern of form k[string]. This gives us worst case time complexity as O(maxK⋅n). |
| 89 | + |
| 90 | +Space Complexity: O(m+n), where mm is the number of letters(a-z) and nn is the number of digits(0-9) in string ss. In worst case, the maximum size of stringStack and countStack could be mm and nn respectively. |
| 91 | +``` |
| 92 | +Tips: |
| 93 | +
|
| 94 | +Use stack to store pair<int, int>(frequency, string). (Equivalent of using two stacks). |
| 95 | +``` |
| 96 | +```c++ |
| 97 | +class Solution { |
| 98 | +public: |
| 99 | + string decodeString(string s) { |
| 100 | + stack<pair<int, string>> decode_stk; |
| 101 | + string ret = ""; |
| 102 | + |
| 103 | + int number = 0; |
| 104 | + for (int i = 0; i < s.size(); i++) { |
| 105 | + if (s[i] == '[') { |
| 106 | + string encode_str = ""; |
| 107 | + decode_stk.push(make_pair(number, encode_str)); |
| 108 | + number = 0; |
| 109 | + } else if (s[i] == ']') { |
| 110 | + pair<int, string> tmp = decode_stk.top(); |
| 111 | + decode_stk.pop(); |
| 112 | + |
| 113 | + string copy = tmp.second; |
| 114 | + while(--tmp.first) { |
| 115 | + copy += tmp.second; |
| 116 | + } |
| 117 | + |
| 118 | + if (decode_stk.empty()) { |
| 119 | + ret += copy; |
| 120 | + } else { |
| 121 | + tmp = decode_stk.top(); |
| 122 | + decode_stk.pop(); |
| 123 | + tmp.second += copy; |
| 124 | + decode_stk.push(tmp); |
| 125 | + } |
| 126 | + } else if (isalpha(s[i])) { |
| 127 | + if (decode_stk.empty()) { |
| 128 | + ret += s[i]; |
| 129 | + } else { |
| 130 | + pair<int, string> tmp = decode_stk.top(); |
| 131 | + decode_stk.pop(); |
| 132 | + tmp.second += s[i]; |
| 133 | + decode_stk.push(tmp); |
| 134 | + } |
| 135 | + } else if (isdigit(s[i])) { |
| 136 | + number = number*10 + s[i] - '0'; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return ret; |
| 141 | + } |
| 142 | +}; |
| 143 | +``` |
0 commit comments