Strings

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note: For the purpose of this problem, we define empty string as valid palindrome. Example 1: Input: "A man, a plan, a canal: Panama" Output: true Example 2: Input: "race a car" Output: false Constraints: * s consists only of printable ASCII characters.

class Solution { public boolean isPalindrome(String s) { } }

First Unique Character in a String Given a string, find the first non-repeating character in it and return its index. If it doesn't exist, return -1. Example: s = "leetcode" return 0. Example 2: s = "loveleetcode" return 2. Note: You may assume the string contains only lowercase English letters.

class Solution { public int firstUniqChar(String s) { } }

Write a function that reverses a string. The input string is given as an array of characters char[]. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. You may assume all the characters consist of printable ascii characters. Example 1: Input: ["h","e","l","l","o"] Output: ["o","l","l","e","h"] Example 2: Input: ["H","a","n","n","a","h"] Output: ["h","a","n","n","a","H"]

Hint 1: The entire logic for reversing a string is based on using the opposite directional two-pointer approach! class Solution { public void reverseString(char[] s) { } }

Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2^31, 2^31 - 1], then return 0. Assume the environment does not allow you to store 64-bit integers (signed or unsigned). Example 1: Input: x = 123 Output: 321 Example 2: Input: x = 120 Output: 21 Example 3: Input: x = -123 Output: -321 Example 4: Input: x = 0 Output: 0 Constraints: * -2^31 <= x <= 2^31 - 1

class Solution { public int reverse(int x) { } }

Count and Say The count-and-say sequence is a sequence of digit strings defined by the recursive formula: * countAndSay(1) = "1" * countAndSay(n) is the way you would "say" the digit string from countAndSay(n-1), which is then converted into a different digit string. To determine how you "say" a digit string, split it into the minimal number of groups so that each group is a contiguous section all of the same character. Then for each group, say the number of characters, then say the character. To convert the saying into a digit string, replace the counts with a number and concatenate every saying. For example, the saying and conversion for digit string "3322251": "two 3's, three 2's, one 5, and one 1" "2 3 + 3 2 + 1 5 + 1 1" "23321511" Given a positive integer n, return the nth term of the count-and-say sequence. Example 1: Input: n = 1 Output: "1" Explanation: This is the base case. Example 2: Input: n = 4 Output: "1211" Explanation: countAndSay(1) = "1" countAndSay(2) = say "1" = one 1 = "11" countAndSay(3) = say "11" = two 1's = "21" countAndSay(4) = say "21" = one 2 + one 1 = "12" + "11" = "1211" Constraints: * 1 <= n <= 30

Hint 1: The following are the terms from n=1 to n=10 of the count-and-say sequence: 1. 1 2. 11 3. 21 4. 1211 5. 111221 6. 312211 7. 13112221 8. 1113213211 9. 31131211131221 10. 13211311123113112211 To generate the nth term, just count and say the n-1th term. class Solution { public String countAndSay(int n) { } }

Longest Common Prefix 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: Input: strs = ["flower","flow","flight"] Output: "fl" Example 2: Input: strs = ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. Constraints: * 0 <= strs.length <= 200 * 0 <= strs[i].length <= 200 * strs[i] consists of only lower-case English letters.

class Solution { public String longestCommonPrefix(String[] strs) { } }

Valid Anagram Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: s = "anagram", t = "nagaram" Output: true Example 2: Input: s = "rat", t = "car" Output: false 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?

class Solution { public boolean isAnagram(String s, String t) { } }

String to Integer (atoi) Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function). The algorithm for myAtoi(string s) is as follows: 1. Read in and ignore any leading whitespace. 2. Check if the next character (if not already at the end of the string) is '-' or '+'. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present. 3. Read in next the characters until the next non-digit charcter or the end of the input is reached. The rest of the string is ignored. 4. Convert these digits into an integer (i.e. "123" -> 123, "0032" -> 32). If no digits were read, then the integer is 0. Change the sign as necessary (from step 2). 5. If the integer is out of the 32-bit signed integer range [-2^31, 2^31 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -2^31 should be clamped to -2^31, and integers greater than 2^31 - 1 should be clamped to 2^31 - 1. 6. Return the integer as the final result. Note: * Only the space character ' ' is considered a whitespace character. * Do not ignore any characters other than the leading whitespace or the rest of the string after the digits. Example 1: Input: s = "42" Output: 42 Explanation: The underlined characters are what is read in, the caret is the current reader position. Step 1: "42" (no characters read because there is no leading ^ whitespace) Step 2: "42" (no characters read because there is neither a '-' nor '+') ^ Step 3: "42" ("42" is read in) ^ The parsed integer is 42. Since 42 is in the range [-2^31, 2^31 - 1], the final result is 42. Example 2: Input: s = " -42" Output: -42 Explanation: Step 1: "______-42" (leading whitespace is read and ignored) ^ Step 2: " -42" ('-' is read, so the result should be negative) ^ Step 3: " -42" ("42" is read in) ^ The parsed integer is -42. Since -42 is in the range [-2^31, 2^31 - 1], the final result is -42. Example 3: Input: s = "4193 with words" Output: 4193 Explanation: Step 1: "4193 with words" (no characters read because there is no ^ leading whitespace) Step 2: "4193 with words" (no characters read because there is neither ^ a '-' nor '+') Step 3: "4193 with words" ("4193" is read in; reading stops because the ^ next character is a non-digit) The parsed integer is 4193. Since 4193 is in the range [-2^31, 2^31 - 1], the final result is 4193. Example 4: Input: s = "words and 987" Output: 0 Explanation: Step 1: "words and 987" (no characters read because there is no ^ leading whitespace) Step 2: "words and 987" (no characters read because there is neither ^ a '-' nor '+') Step 3: "words and 987" (reading stops immediately because there is ^ a non-digit 'w') The parsed integer is 0 because no digits were read. Since 0 is in the range [-2^31, 2^31 - 1], the final result is 0. Example 5: Input: s = "-91283472332" Output: -2147483648 Explanation: Step 1: "-91283472332" (no characters read because there is no ^ leading whitespace) Step 2: "-91283472332" ('-' is read, so the result should be negative) ^ Step 3: "-91283472332" ("91283472332" is read in) ^ The parsed integer is -91283472332. Since -91283472332 is less than the lower bound of the range [-2^31, 2^31 - 1], the final result is clamped to -231 = -2147483648. Constraints: * 0 <= s.length <= 200 * s consists of English letters (lower-case and upper-case), digits (0-9), ' ', '+', '-', and '.'. https://leetcode.com/explore/interview/card/top-interview-questions-easy/127/strings/884/

class Solution { public int myAtoi(String s) { } }

Implement strStr() Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 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(). Example 1: Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Input: haystack = "aaaaa", needle = "bba" Output: -1 Example 3: Input: haystack = "", needle = "" Output: 0 Constraints: * 0 <= haystack.length, needle.length <= 5 * 104 * haystack and needle consist of only lower-case English characters.

class Solution { public int strStr(String haystack, String needle) { } }


Ensembles d'études connexes

Set 3 : hyperbole - loose sentence

View Set

Clinical Exercise Physiology Final Exam

View Set

Prime Factorization of numbers 1-100

View Set

Sample Exam ISTQB Agile Foundation Questions - ASTQB - OFFICIAL EXAM

View Set