Leetcode - Strings Simple
14. Longest Common Prefix
https://leetcode.com/problems/implement-strstr/
Description
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Example 1:
1 | Input: ["flower","flow","flight"] |
Example 2:
1 | Input: ["dog","racecar","car"] |
Note:
All given inputs are in lowercase letters a-z.
Solution
1 | class Solution: |
https://leetcode.com/discuss/89987/one-line-solution-using-itertools-takewhile
1 | def longestCommonPrefix(self, strs): |
28. Implement strStr()
https://leetcode.com/problems/implement-strstr/
Description
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
1 | Input: haystack = "hello", needle = "ll" |
Example 2:
1 | Input: haystack = "aaaaa", needle = "bba" |
Clarification:
What should we return when needle is an empty string? This is a great question to ask during an interview.
For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C’s strstr() and Java’s indexOf().
Solution
Use built-in functions
haystack.find(needle) or
1 | class Solution: |
242. Valid Anagram[E]
https://leetcode.com/problems/valid-anagram/
Description
Given two strings s and t , write a function to determine if t is an anagram of s.
Example 1:
1 | Input: s = "anagram", t = "nagaram" |
Example 2:
1 | Input: s = "rat", t = "car" |
Note:
You may assume the string contains only lowercase alphabets.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
Solution
https://leetcode.com/problems/valid-anagram/solution/
1 |
383. Ransom Note[E]
https://leetcode.com/problems/ransom-note/
Description
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Example 1:
1 | Input: ransomNote = "a", magazine = "b" |
Example 2:
1 | Input: ransomNote = "aa", magazine = "ab" |
Example 3:
1 | Input: ransomNote = "aa", magazine = "aab" |
Constraints:
- You may assume that both strings contain only lowercase letters.
Solution
1 | class Solution: |
387. First Unique Character in a String[E]
https://leetcode.com/problems/first-unique-character-in-a-string/
Description
Given a string, find the first non-repeating character in it and return its index. If it doesn’t exist, return -1.
Examples:
1 | s = "leetcode" |
Note: You may assume the string contains only lowercase English letters.
Solution
1 | class Solution: |
389. Find the Difference[E]
https://leetcode.com/problems/find-the-difference/
Description
Given two strings *s* and *t* which consist of only lowercase letters.
String *t* is generated by random shuffling string *s* and then add one more letter at a random position.
Find the letter that was added in *t*.
Example:
1 | Input: |
Solution
1 | class Solution: |
415. Add Strings[E]
https://leetcode.com/problems/add-strings/
Description
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
- The length of both
num1andnum2is < 5100. - Both
num1andnum2contains only digits0-9. - Both
num1andnum2does not contain any leading zero. - You must not use any built-in BigInteger library or convert the inputs to integer directly.
Solution
1 |
438. Find All Anagrams in a String[M]
https://leetcode.com/problems/find-all-anagrams-in-a-string/
Description
iven a string s and a non-empty string p, find all the start indices of p’s anagrams in s.
Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.
The order of output does not matter.
Example 1:
1 | Input: |
Example 2:
1 | Input: |
Solution
1 |
459. Repeated Substring Pattern
https://leetcode.com/problems/repeated-substring-pattern/
Description
Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.
Example 1:
1 | Input: "abab" |
Example 2:
1 | Input: "aba" |
Example 3:
1 | Input: "abcabcabcabc" |
Solution
hahaha
1 | class Solution: |
709. To Lower Cases[E]
https://leetcode.com/problems/to-lower-case/
Description
Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
Example 1:
1 | Input: "Hello" |
Example 2:
1 | Input: "here" |
Example 3:
1 | Input: "LOVELY" |
Solution
1 | class Solution: |
771. Jewels and Stones[E]
https://leetcode.com/problems/jewels-and-stones/
Description
You’re given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.
The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".
Example 1:
1 | Input: J = "aA", S = "aAAbbbb" |
Example 2:
1 | Input: J = "z", S = "ZZ" |
Note:
SandJwill consist of letters and have length at most 50.- The characters in
Jare distinct.
Solution
1 | class Solution: |
811. Subdomain Visit Count[E]
https://leetcode.com/problems/subdomain-visit-count/
Description
A website domain like “discuss.leetcode.com” consists of various subdomains. At the top level, we have “com”, at the next level, we have “leetcode.com”, and at the lowest level, “discuss.leetcode.com”. When we visit a domain like “discuss.leetcode.com”, we will also visit the parent domains “leetcode.com” and “com” implicitly.
Now, call a “count-paired domain” to be a count (representing the number of visits this domain received), followed by a space, followed by the address. An example of a count-paired domain might be “9001 discuss.leetcode.com”.
We are given a list cpdomains of count-paired domains. We would like a list of count-paired domains, (in the same format as the input, and in any order), that explicitly counts the number of visits to each subdomain.
1 | Example 1: |
Notes:
- The length of
cpdomainswill not exceed100. - The length of each domain name will not exceed
100. - Each address will have either 1 or 2 “.” characters.
- The input count in any count-paired domain will not exceed
10000. - The answer output can be returned in any order.
Solution
https://leetcode.com/problems/subdomain-visit-count/solution/
ttps://leetcode.com/problems/subdomain-visit-count/discuss/121770/Python-short-and-understandable-solution-68-ms
1 |
844. Backspace String Compare[E]
https://leetcode.com/problems/backspace-string-compare/
Description
Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.
Note that after backspacing an empty text, the text will continue empty.
Example 1:
1 | Input: S = "ab#c", T = "ad#c" |
Example 2:
1 | Input: S = "ab##", T = "c#d#" |
Example 3:
1 | Input: S = "a##c", T = "#a#c" |
Example 4:
1 | Input: S = "a#c", T = "b" |
Note:
1 <= S.length <= 2001 <= T.length <= 200SandTonly contain lowercase letters and'#'characters.
Follow up:
- Can you solve it in
O(N)time andO(1)space?
Solution
https://leetcode.com/problems/backspace-string-compare/solution/
1 |
929. Unique Email Addresses[E]
https://leetcode.com/problems/unique-email-addresses/
Description
Every email consists of a local name and a domain name, separated by the @ sign.
For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain name.
Besides lowercase letters, these emails may contain '.'s or '+'s.
If you add periods ('.') between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address. (Note that this rule does not apply for domain names.)
If you add a plus ('+') in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered, for example m.y+name@email.com will be forwarded to my@email.com. (Again, this rule does not apply for domain names.)
It is possible to use both of these rules at the same time.
Given a list of emails, we send one email to each address in the list. How many different addresses actually receive mails?
Example 1:
1 | Input: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"] |
Note:
1 <= emails[i].length <= 1001 <= emails.length <= 100- Each
emails[i]contains exactly one'@'character. - All local and domain names are non-empty.
- Local names do not start with a
'+'character.
Solution
https://leetcode.com/problems/unique-email-addresses/solution/
1 |
937. Reorder Data in Log Files[E]
https://leetcode.com/problems/reorder-data-in-log-files/
Description
You have an array of logs. Each log is a space delimited string of words.
For each log, the first word in each log is an alphanumeric identifier. Then, either:
- Each word after the identifier will consist only of lowercase letters, or;
- Each word after the identifier will consist only of digits.
We will call these two varieties of logs letter-logs and digit-logs. It is guaranteed that each log has at least one word after its identifier.
Reorder the logs so that all of the letter-logs come before any digit-log. The letter-logs are ordered lexicographically ignoring identifier, with the identifier used in case of ties. The digit-logs should be put in their original order.
Return the final order of the logs.
Example 1:
1 | Input: logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"] |
Constraints:
0 <= logs.length <= 1003 <= logs[i].length <= 100logs[i]is guaranteed to have an identifier, and a word after the identifier.
Solution
https://leetcode.com/problems/reorder-data-in-log-files/solution/
1 |
953. Verifying an Alien Dictionary[E]
https://leetcode.com/problems/verifying-an-alien-dictionary/
Description
In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.
Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographicaly in this alien language.
Example 1:
1 | Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz" |
Example 2:
1 | Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz" |
Example 3:
1 | Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz" |
Constraints:
1 <= words.length <= 1001 <= words[i].length <= 20order.length == 26- All characters in
words[i]andorderare English lowercase letters.
Solution
https://leetcode.com/problems/verifying-an-alien-dictionary/solution/
1 |





