 # String - 68. Text Justification

68. Text Justification Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly maxWidth characters. Extra spaces between words should be distributed as evenly as possible. If the number of spac.... # String - 273. Integer to English Words

273. Integer to English Words Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123 Output: “One Hundred Twenty Three” Example 2: Input: 12345 Output: “Twelve Thousand Three Hundred Forty Five” 思路： 把数字按照英文输出，这和有一题读阿拉伯数字是类似的，就是一个实现题，思想就是把"Thousand", "Million", "Billion"作为单位，分段计算每一段应该读做多少，然后加上这个单位就可以。 代码： java： class Solution { private static final String[] LESS_THAN_20 = {"", "One", "Two", "Thre.... # String - 12. Integer to Roman

12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. **Symbol** **Value** I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually.... # String - 13. Roman to Integer

13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. **Symbol** **Value** I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually.... # String - 171. Excel Sheet Column Number

171. Excel Sheet Column Number Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ... Example 1: Input: “A” Output: 1 思路： 这一 题是168的逆过程实质就是26进制转换为10进制。 代码： java： class Solution { public int titleToNumber(String s) { int res = 0, i = 0; while (i < s.length())res = res * 26 + s.charAt(i++) - 'A' + 1; return res; } } # String - 168. Excel Sheet Column Title

168. Excel Sheet Column Title Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ... Example 1: Input: 1 Output: “A” 思路： 对于输入n进行取模，来确定这一位应该是什么字母，再计算剩下的数。依次类推，需要注意A是第一个，所以对n先减一再取模。 代码： java： class Solution { public String convertToTitle(int n) { StringBuilder sb = new StringBuilder(); while (n > 0) { n--; sb.append((char)('A' + n % 26)); n /= 26; } sb.reverse().... # String - 316. Remove Duplicate Letters

316. Remove Duplicate Letters Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example 1: Input: “bcabc” Output: “abc” Example 2: Input: “cbacdcbc” Output: “acdb” 思路： 题目意思是指移除字符串中重复的数字，剩下的字符串是按字典序排序的。可以先遍历一遍数组，记录每个字母出现的次数，然后根据这个数组去组装字符串res，使得这个字符串是字典序，具体细节就是，对于某一位字符，去判断输出结果的最后一位和当前字符大小，以及后面还有没有这个字符。如果说，res的最后一位.... # String - 38. Count and Say

38. Count and Say The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" or 1211. Given an integer n where 1 ≤ n ≤ 30, generate the _n_th term of the count-and-say sequence. Note: Each term of the sequence of integers wi.... # String - 161. One Edit Distance

One Edit Distance Given two strings S and T, determine if they are both one edit distance apart. Have you met this question in a real interview?  Yes Problem Correction Example Example 1: Input: s = "aDb", t = "adb" Output: true Example 2: Input: s = "ab", t = "ab" Output: false Explanation: s=t ,so they aren't one edit distance apart 思路： 题目意思是两个字符串，通过一次替换或者增加或者删除操作，使得两个字符串相等。增加和删除其实属于一类，因为相对于短的字符串是增加，而长的字符串是减少，所以问题分类为两类。一类是替换，一类是增加删除。替换的在一次遍历的时候，只允许相同位置，出现一次字符不相同，增加和删除也是类似，找出相邻字符不.... # String - 6. ZigZag Conversion

ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: string convert(string s, int numRows); Example 1: Input: s = “PAYPALISHIRING”, numRows = 3 Output: “PAHNAPLSIIGYIR” 思路： 遍历字符串，找出.... # String - 179. Largest Number

179. Largest Number Given a list of non negative integers, arrange them such that they form the largest number. Example 1: Input: [10,2] Output: “210” Example 2: Input: [3,30,34,5,9] Output: “9534330” Note: The result may be very large, so you need to return a string instead of an integer. 思路： 贪心的思想，比较相邻两个字符串拼接结果，比较大小。 代码： java： class Solution { public String largestNumber(int[] nums) { if (nums == null || nums.length == 0) return ""; int len = nums.length; String [] strs = new S.... # String - 49. Group Anagrams

49. Group Anagrams Given an array of strings, group anagrams together. Example: Input: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”], Output: [ [“ate”,“eat”,“tea”], [“nat”,“tan”], [“bat”] ] Note: All inputs will be in lowercase. The order of your output does not matter. 思路： 问题主要是如何选出字符串数组中同构异形的字符串，所以制造一个唯一标识，或者对每一个字符串进行排序，加上O(1)操作的map来找出相同的字符串。 代码： java： class Solution { public List<List<String>> groupAnagrams(String[] strs) { List<List<String>> res = new Arr.... # String - 242. Valid Anagram

242. 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? 思路： 记录第一个数组每个元素出现的次数，然后去对比另一个数组。 代码： java： class Solution { /*pu.... # String - 290. Word Pattern

290. Word Pattern 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? 思路： 题目的实质是两个数组相同元素的索引匹配，可以抽象为两个数组，分别记录某个元素在源字符串中的下标。这里用map来做，因为put.... # String - 205. Isomorphic Strings

205. Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself. Example 1: Input: s = “egg”, t = “add” Output: true Example 2: Input: s = “foo”, t = “bar” Output: false .... # String - 345. Reverse Vowels of a String

345. Reverse Vowels of a String Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Input: “hello” Output: “holle” Example 2: Input: “leetcode” Output: “leotcede” Note: The vowels does not include the letter “y”. 思路： 只翻转元音字母，就只用两个指针，首尾移动来翻转元音字母。 代码： java： class Solution { public static boolean[] vowels = new boolean; static{ vowels['a'] = true; vowels['o'] = true; vowels['e'] = true; vowels['i'] = true; vowels['u'] = true; vowels['A'] =.... # String - 186. Reverse Words in a String II

Reverse Words in a String II Given an input character array, reverse the array word by word. A word is defined as a sequence of non-space characters. The input character array does not contain leading or trailing spaces and the words are always separated by a single space. Have you met this question in a real interview?  Yes Problem Correction Example Example1 Input: s = "the sky is blue" Output: "blue is sky the" Example2 Input: "a b c" Output: "c b a" Challenge Could you do i.... # String - 151. Reverse Words in a String

151. Reverse Words in a String Given an input string, reverse the string word by word. Example 1: Input: “the sky is blue” **Output: **“blue is sky the” Example 2: Input: "  hello world!  " **Output: **“world! hello” Explanation: Your reversed string should not contain leading or trailing spaces. Example 3: Input: “a good   example” **Output: **“example good a” Explanation: You need to reduce multiple spaces between two words to a single space in the rever.... # String - 344. Reverse String

344. Reverse String 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”] 思路： two pointers 代码： java： class Solution { public void reverseString(char[].... # String - 383. Ransom Note

383. Ransom Note 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. Note: You may assume that both strings contain only lowercase letters. canConstruct(“a”, “b”) -> false canConstruct(“aa”, “ab”) -> false canConstruct(“aa”, “aab”)....

Nothing just happens, it's all part of a plan.