Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .github/lc_chart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*__pycache__
**/revision.txt
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ The following diagram illustrates the action flow of using these [scripts](scrip
|**66**|🟩 Easy|⚙️|[Plus One](https://lcid.cc/66)|<a href="python/lce_p0066_plus_one.py"><img src=".github/icons/python-original.svg" height="25"></a>|
|**73**|🟨 Medium|⚙️|[Set Matrix Zeroes](https://lcid.cc/73)|<a href="python/lcm_p0073_set_matrix_zeroes.py"><img src=".github/icons/python-original.svg" height="25"></a>|
|**77**|🟨 Medium|⚙️|[Combinations](https://lcid.cc/77)|<a href="java/lcm_p0077_combinations.java"><img src=".github/icons/java-original.svg" height="25"></a>|
|**125**|🟩 Easy|⚙️|[Valid Palindrome](https://lcid.cc/125)|<a href="java/lce_p0125_valid_palindrome.java"><img src=".github/icons/java-original.svg" height="25"></a>|
|**128**|🟨 Medium|⚙️|[Longest Consecutive Sequence](https://lcid.cc/128)|<a href="java/lcm_p0128_longest_consecutive_sequence.java"><img src=".github/icons/java-original.svg" height="25"></a>|
|**136**|🟩 Easy|⚙️|[Single Number](https://lcid.cc/136)|<a href="java/lce_p0136_single_number.java"><img src=".github/icons/java-original.svg" height="25"></a>|
|**151**|🟨 Medium|⚙️|[Reverse Words In A String](https://lcid.cc/151)|<a href="python/lcm_p0151_reverse_words_in_a_string.py"><img src=".github/icons/python-original.svg" height="25"></a>|
|**151**|🟨 Medium|⚙️|[Reverse Words In A String](https://lcid.cc/151)|<a href="java/lcm_p0151_reverse_words_in_a_string.java"><img src=".github/icons/java-original.svg" height="25"></a> <a href="python/lcm_p0151_reverse_words_in_a_string.py"><img src=".github/icons/python-original.svg" height="25"></a>|
|**175**|🟩 Easy|🛢️|[Combine Two Tables](https://lcid.cc/175)|<a href="sql/lce_p0175_combine_two_tables_mysql.sql"><img src=".github/icons/mysql-original.svg" height="25"></a> <a href="sql/lce_p0175_combine_two_tables_postgresql.sql"><img src=".github/icons/postgresql-original.svg" height="25"></a>|
|**176**|🟨 Medium|🛢️|[Second Highest Salary](https://lcid.cc/176)|<a href="sql/lcm_p0176_second_highest_salary_mysql.sql"><img src=".github/icons/mysql-original.svg" height="25"></a>|
|**178**|🟨 Medium|🛢️|[Rank Scores](https://lcid.cc/178)|<a href="sql/lcm_p0178_rank_scores_mysql.sql"><img src=".github/icons/mysql-original.svg" height="25"></a>|
Expand All @@ -49,6 +50,7 @@ The following diagram illustrates the action flow of using these [scripts](scrip
|**230**|🟨 Medium|⚙️|[Kth Smallest Element In A Bst](https://lcid.cc/230)|<a href="python/lcm_p0230_kth_smallest_element_in_a_bst.py"><img src=".github/icons/python-original.svg" height="25"></a>|
|**238**|🟨 Medium|⚙️|[Product Of Array Except Self](https://lcid.cc/238)|<a href="java/lcm_p0238_product_of_array_except_self.java"><img src=".github/icons/java-original.svg" height="25"></a> <a href="python/lcm_p0238_product_of_array_except_self.py"><img src=".github/icons/python-original.svg" height="25"></a>|
|**242**|🟩 Easy|⚙️|[Valid Anagram](https://lcid.cc/242)|<a href="java/lce_p0242_valid_anagram.java"><img src=".github/icons/java-original.svg" height="25"></a> <a href="python/lce_p0242_valid_anagram.py"><img src=".github/icons/python-original.svg" height="25"></a>|
|**268**|🟩 Easy|⚙️|[Missing Number](https://lcid.cc/268)|<a href="java/lce_p0268_missing_number.java"><img src=".github/icons/java-original.svg" height="25"></a>|
|**271**|🟨 Medium|⚙️|[Encode And Decode Strings](https://lcid.cc/271)|<a href="java/lcm_p0271_encode_and_decode_strings.java"><img src=".github/icons/java-original.svg" height="25"></a>|
|**283**|🟩 Easy|⚙️|[Move Zeroes](https://lcid.cc/283)|<a href="python/lce_p0283_move_zeroes.py"><img src=".github/icons/python-original.svg" height="25"></a>|
|**334**|🟨 Medium|⚙️|[Increasing Triplet Subsequence](https://lcid.cc/334)|<a href="python/lcm_p0334_increasing_triplet_subsequence.py"><img src=".github/icons/python-original.svg" height="25"></a>|
Expand Down Expand Up @@ -146,6 +148,7 @@ The following diagram illustrates the action flow of using these [scripts](scrip
|**1672**|🟩 Easy|⚙️|[Richest Customer Wealth](https://lcid.cc/1672)|<a href="python/lce_p1672_richest_customer_wealth.py"><img src=".github/icons/python-original.svg" height="25"></a>|
|**1679**|🟨 Medium|⚙️|[Max Number Of K-sum Pairs](https://lcid.cc/1679)|<a href="python/lcm_p1679_max_number_of_k-sum_pairs.py"><img src=".github/icons/python-original.svg" height="25"></a>|
|**1683**|🟩 Easy|🛢️|[Invalid Tweets](https://lcid.cc/1683)|<a href="sql/lce_p1683_invalid_tweets_sqlserver.sql"><img src=".github/icons/sqlserver-original.svg" height="25"></a>|
|**1684**|🟩 Easy|⚙️|[Count The Number Of Consistent Strings](https://lcid.cc/1684)|<a href="java/lce_p1684_count_the_number_of_consistent_strings.java"><img src=".github/icons/java-original.svg" height="25"></a>|
|**1693**|🟩 Easy|🛢️|[Daily Leads And Partners](https://lcid.cc/1693)|<a href="sql/lce_p1693_daily_leads_and_partners_mysql.sql"><img src=".github/icons/mysql-original.svg" height="25"></a>|
|**1700**|🟩 Easy|⚙️|[Number Of Students Unable To Eat Lunch](https://lcid.cc/1700)|<a href="python/lce_p1700_number_of_students_unable_to_eat_lunch.py"><img src=".github/icons/python-original.svg" height="25"></a>|
|**1720**|🟩 Easy|⚙️|[Decode Xored Array](https://lcid.cc/1720)|<a href="python/lce_p1720_decode_xored_array.py"><img src=".github/icons/python-original.svg" height="25"></a>|
Expand Down
119 changes: 119 additions & 0 deletions java/lce_p0125_valid_palindrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
LCE 125. Valid Palindrome

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward.
Alphanumeric characters include letters and numbers.

Given a string s, return true if it is a palindrome, or false otherwise.

Constraints:
- 1 <= s.length <= 2 * 10^5
- s consists only of printable ASCII characters.

Topics: Two Pointers, String
*/

class Solution {

// Time Complexity: O(n) - 1 ms -> 100.00%
// Space Complexity: O(n) - 43.11 MB -> 69.00%
public boolean isPalindrome(String s) {

int startPtr = 0;
int endPtr = s.length() - 1;

while (startPtr < endPtr) {

char startChar = s.charAt(startPtr);
char endChar = s.charAt(endPtr);

if (startChar >= 'A' && startChar <= 'Z') startChar = (char) (startChar - 'A' + 'a');
if (endChar >= 'A' && endChar <= 'Z') endChar = (char) (endChar - 'A' + 'a');

if (!((startChar >= 'a' && startChar <= 'z') || (startChar >= '0' && startChar <= '9'))) {
startPtr++;
continue;
}
else if (!((endChar >= 'a' && endChar <= 'z') || (endChar >= '0' && endChar <= '9'))) {
endPtr--;
continue;
}
else {
if (startChar != endChar) {
return false;
}

startPtr++;
endPtr--;
}
}

return true;
}

// Time Complexity: O(n) - 4 ms -> 55.36%
// Space Complexity: O(n) - 43.68 MB -> 52.39%
public boolean isPalindrome2(String s) {

// as per first requirement
s = getCleanedString(s);

int n = s.length();

// case 1: string length 0 ("") or 1 ("a")
if (n <= 1) {
return true;
}

// case 2: string length 2 ("ab" or "aa")
if (n == 2) {
return s.charAt(0) == s.charAt(1);
}

// case 3: string length >= 3
int startPtr = 0;
int endPtr = n - 1;

while (startPtr < endPtr) {

char startChar = s.charAt(startPtr);
char endChar = s.charAt(endPtr);

if (startChar != endChar) {
return false;
}

startPtr++;
endPtr--;
}

return true;
}

private String getCleanedString(String s) {

return s.replaceAll("[^a-zA-Z0-9]+", "").toLowerCase();
}

private String getCleanedStringII(String s) {

StringBuilder sb = new StringBuilder();

for (char ch : s.toCharArray()) {
if (Character.isDigit(ch)) {
sb.append(ch);
}
if (Character.isLetter(ch)) {
sb.append(Character.toLowerCase(ch));
}
}

return sb.toString();
}
}

/*
methods:
1. single pass - first skip ch and advance ptrs if it is not alphanumeric for ch at startPtr / endPtr, if both are OK then compare them
2. double pass - 1st pass - clean the string as per requirements, 2nd pass - check ch at startPtr / endPtr
*/
2 changes: 1 addition & 1 deletion java/lce_p0242_valid_anagram.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public boolean isAnagramAlt2(String s, String t) {
char[] charT = t.toCharArray();
Arrays.sort(charS);
Arrays.sort(charT);

return Arrays.equals(charS, charT);
}
}
Expand Down
46 changes: 46 additions & 0 deletions java/lce_p0268_missing_number.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
LCE 268. Missing Number

Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.

Constraints:
- n == nums.length
- 1 <= n <= 10^4
- 0 <= nums[i] <= n
- All the numbers of nums are unique.

Topics: Array, Hash Table, Math, Binary Search, Bit Manipulation, Sorting
*/

class Solution {

/*
- XOR is commutative: A ^ B = B ^ A
- XOR is associative: (A ^ B) ^ C = A ^ (B ^ C)
- XORing something twice removes it: A ^ A = 0
- XORing with zero does nothing: A ^ 0 = A
*/

public int missingNumber(int[] nums) {

int n = nums.length;

int firstSumXOR = 0;
int secondSumXOR = 0;

// e.g. all in [0, n] => [0, 1, 2, 3]
for (int i = 0; i <= n; i++) {
firstSumXOR ^= i;
}

// e.g. nums => [3, 0, 1]
for (int num : nums) {
secondSumXOR ^= num;
}

return firstSumXOR ^ secondSumXOR; // 2
}
}

// Time Complexity: O(n) - 0 ms -> 100.00%
// Space Complexity: O(1) - 45.26 MB -> 40.66%
42 changes: 42 additions & 0 deletions java/lce_p1684_count_the_number_of_consistent_strings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
LCE 1684. Count the Number of Consistent Strings

You are given a string 'allowed' consisting of distinct characters and an array of strings 'words'.
A string is consistent if all characters in the string appear in the string 'allowed'.

Return the number of consistent strings in the array 'words'.

Constraints:
- n == nums.length
- 1 <= n <= 10^4
- 0 <= nums[i] <= n
- All the numbers of nums are unique.

Topics: Array, Hash Table, String, Bit Manipulation, Counting
*/

class Solution {
public int countConsistentStrings(String allowed, String[] words) {

boolean[] allowedChars = new boolean[26];
for (char ch : allowed.toCharArray()) {
allowedChars[ch - 'a'] = true;
}

int count = words.length;
for (String word : words) {

for (char ch : word.toCharArray()) {
if (!allowedChars[ch - 'a']) {
count--;
break;
}
}
}

return count;
}
}

// Time Complexity: O(n^2) - 6 ms -> 82.59%
// Space Complexity: O(1) - 45.27 MB -> 65.85%
2 changes: 1 addition & 1 deletion java/lcm_p0049_group_anagrams.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Given an array of strings strs, group the anagrams together. You can return the answer in any order.

Constraints:
- 1 <= strs.length <= 104
- 1 <= strs.length <= 10^4
- 0 <= strs[i].length <= 100
- strs[i] consists of lowercase English letters.

Expand Down
6 changes: 3 additions & 3 deletions java/lcm_p0128_longest_consecutive_sequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public int longestConsecutive(int[] nums) {

Set<Integer> set = new HashSet<>();
for (int num : nums) set.add(num);

for (int num : nums) {

// process only starting numbers
Expand All @@ -44,10 +44,10 @@ public int longestConsecutive(int[] nums) {
maxLength = Math.max(currLength, maxLength);
}
}

return maxLength;
}

// Time Complexity: O(n log n) - 16 ms -> 91.59%
// Space Complexity: O(1) - 56.8 MB -> 81.76%
public int longestConsecutiveAlt(int[] nums) {
Expand Down
48 changes: 48 additions & 0 deletions java/lcm_p0151_reverse_words_in_a_string.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
LCM 151. Reverse Words in a String

Given an input string s, reverse the order of the words.

A word is defined as a sequence of non-space characters.
The words in s will be separated by at least one space.

Return a string of the words in reverse order concatenated by a single space.

Note that s may contain leading or trailing spaces or multiple spaces between two words.
The returned string should only have a single space separating the words. Do not include any extra spaces.

Constraints:
- 1 <= s.length <= 10^4
- s contains English letters (upper-case and lower-case), digits, and spaces ' '.
- There is at least one word in s.

Topics: Two Pointers, String
*/

class Solution {
public String reverseWords(String s) {

// note: "\\s+" matches sequence of one or more whitespace characters.
String[] words = s.trim().split("\\s+");

reverseArray(words);

return String.join(" ", words);
}

// alternative: use Collections.reverse(List<T>) instead.
private void reverseArray(String[] words) {

int n = words.length;

for (int i = 0; i < n / 2; i++) {
String temp = words[i];
words[i] = words[n - i - 1];
words[n - i - 1] = temp;
}
}
}

// Time Complexity: O(L) - 8 ms -> 39.40%
// Space Complexity: O(L) - 42.65 MB -> 94.35%
// L => length of string s