
字符串
字符串专题
wdt_
即使上线也匆忙,新版本,愁断肠,orz...
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
【字符串】A058_LC_最短回文串(找逆序串的最短后缀)
给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。示例 1:输入: "aacecaaa"输出: "aaacecaaa"示例 2:输入: "abcd"输出: "dcbabcd"方法一:最烦的就是可以添加字符串,添加的字符不难想到,即:要使 s 为回文串,那添加的那若干个字符一定是与 s 的后半部分的几个字符相同,故可先得到 s 的翻转串 rs,然后在 rs 中找一个最短的后缀 suf_re,如果 suf_rs 是 s 的子串,则非原创 2020-08-30 00:10:25 · 367 阅读 · 0 评论 -
【字符串】C057_LQ_Fibonacci 数列与黄金分割 & 生成回文数 & 复数幂(大数除法+大数保留n为小数)
Fibonacci 数列是非常著名的数列:F[1] = 1,F[2] = 1,对于 i > 3,F[i] = F[i − 1] + F[i − 2]Fibonacci 数列有一个特殊的性质,前一项与后一项的比值,F[i]/F[i + 1], 会趋近于黄金分割。为了验证这一性质,给定正整数 N,请你计算 F[N]/F[N + 1],并保留 8 位 小数。输入一个正整数 N。(1 ≤ N ≤ 2000000000)输出F[N]/F[N + 1]。答案保留 8 位小数。样例输入2样例原创 2020-08-26 20:57:18 · 354 阅读 · 0 评论 -
【字符串】B056_LQ_切开字符串(暴力取子串 / 反向思维+累加思想)
Pear有一个字符串,不过他希望把它切成两段。这是一个长度为N(<=10^5)的字符串。Pear希望选择一个位置,把字符串不重复不遗漏地切成两段,长度分别是t和N-t(这两段都必须非空)。Pear用如下方式评估切割的方案:定义“正回文子串”为:长度为奇数的回文子串。设切成的两段字符串中,前一段中有A个不相同的正回文子串,后一段中有B个不相同的非正回文子串,则该方案的得分为A*B。注意,后一段中的B表示的是:“…非正回文…”,而不是: “…正回文…”。那么所有的切割方案中,A*B的最大值是原创 2020-08-17 18:21:12 · 212 阅读 · 0 评论 -
【数组】C008_LC_删列造序(枚举......)
一、题目描述输入:["cba", "daf", "ghi"]输出:1解释:当选择 D = {1},删除后 A 的列为:["c","d","g"] 和 ["a","f","i"],均为非降序排列。若选择 D = {},那么 A 的列 ["b","a","h"] 就不是非降序排列了。二、题解方法一:枚举枚举每一列,如果遇到 grid[r-1][c] > grid[r][c] ...原创 2020-04-06 17:04:20 · 176 阅读 · 0 评论 -
【字符串】C055_LC_独特的电子邮件地址(模拟)
一、Problem每封电子邮件都由一个本地名称和一个域名组成,以 @ 符号分隔。例如,在 [email protected]中, alice 是本地名称,而 leetcode.com 是域名。除了小写字母,这些电子邮件还可能包含 ‘.’ 或 ‘+’。如果在电子邮件地址的本地名称部分中的某些字符之间添加句点(’.’),则发往那里的邮件将会转发到本地名称中没有点的同一地址。例如,"[email protected]” 和 “[email protected]” 会转发到同一电子邮件地址。 (请注原创 2020-07-10 17:26:09 · 1767 阅读 · 0 评论 -
【字符串】B054_LC_困于环中的机器人(模拟 / 取模控制方向)
一、Problem二、Solution方法一:"GL"输出:false预期:trueclass Solution { final static int U = 0, D = 1, L = 2, R = 3; public boolean isRobotBounded(String instructions) { char s[] = instructions.toCharArray(); int x = 0, y = 0, d = U; Set&原创 2020-06-27 11:17:09 · 340 阅读 · 0 评论 -
【字符串】A052_LC_按字典序排在最后的子串(最大后缀)
一、ProblemGiven a string s, return the last substring of s in lexicographical order.Output: "bab"Explanation: The substrings are ["a", "ab", "aba", "abab", "b", "ba", "bab"]. The lexicographically maximum substring is "bab".Note:1 <= s.length <原创 2020-06-20 17:46:05 · 475 阅读 · 0 评论 -
【字符串】B050_LC_删除子文件夹(排序 + 记录根文件夹 / trie 树优化)
一、Problem你是一位系统管理员,手里有一份文件夹列表 folder,你的任务是要删除该列表中的所有 子文件夹,并以 任意顺序 返回剩下的文件夹。我们这样定义「子文件夹」:如果文件夹 folder[i] 位于另一个文件夹 folder[j] 下,那么 folder[i] 就是 folder[j] 的子文件夹。文件夹的「路径」是由一个或多个按以下格式串联形成的字符串:/ 后跟一个或者多个小写英文字母。例如,/leetcode 和 /leetcode/problems 都是有效的路径,而空字符串原创 2020-06-18 23:26:22 · 245 阅读 · 0 评论 -
【字符串】B049_LC_合并内容流(模拟)
一、Problem合并两个内容流,实现隔4个插入1个,如果合并完还有剩下,则加内容流尾部输入描述:第行表示第一种类型的内容,字符数量<=100,空格分隔。比如说1 2 3 4 5 6 7 8 9a b c输出描述:合并两种内容流,输出1 2 3 4 a 5 6 7 8 b 9 c二、Solution方法一:模拟注意边界即可…import java.util.*;import java.math.*;import java.io.*;public class Main原创 2020-06-18 11:26:25 · 207 阅读 · 0 评论 -
【字符串】A050_LC_子串的最大出现次数(暴力 / 字符串 hash)
一、ProblemReturn the number of distinct non-empty substrings of text that can be written as the concatenation of some string with itself (i.e. it can be written as a + a where a is some string).Input: text = "abcabcabc"Output: 3Explanation: The 3 substr原创 2020-06-09 10:32:34 · 245 阅读 · 0 评论 -
【字符串】B049_LC_子串的最大出现次数(暴力 / 位运算)
一、ProblemGiven a string s, return the maximum number of ocurrences of any substring under the following rules:The number of unique characters in the substring must be less than or equal to maxLetters.The substring size must be between minSize and maxSiz原创 2020-06-06 08:22:03 · 202 阅读 · 0 评论 -
【字符串】B048_LC_检查一个字符串是否包含所有长度为 K 的二进制子串(暴力 / 优化 / 回溯)
一、ProblemGiven a binary string s and an integer k.Return True if any binary code of length k is a substring of s. Otherwise, return False.Input: s = "00110110", k = 2Output: trueExplanation: The binary codes of length 2 are "00", "01", "10" and "11".原创 2020-05-31 08:16:19 · 462 阅读 · 0 评论 -
【字符串】B044_LC_每个元音包含偶数次的最长子字符串(状态压缩)
一、ProblemGiven the string s, return the size of the longest substring containing each vowel an even number of times. That is, ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’ must appear an even number of times.Input: s = "eleetminicoworoep"Output: 13Explanation: The longe原创 2020-05-20 22:19:26 · 573 阅读 · 0 评论 -
【字符串】B043_LC_包含所有三种字符的子字符串数目(双指针)
一、ProblemGiven a string s consisting only of characters a, b and c.Return the number of substrings containing at least one occurrence of all these characters a, b and c.Input: s = "abcabc"Output: 10Explanation: The substrings containing at least one o原创 2020-05-20 16:24:32 · 296 阅读 · 0 评论 -
【字符串】B039_LC_收藏清单(暴力 / set 优化 / 逻辑优化)
一、ProblemGiven the array favoriteCompanies where favoriteCompanies[i] is the list of favorites companies for the ith person (indexed from 0).Return the indices of people whose list of favorite companies is not a subset of any other list of favorites comp原创 2020-05-17 15:53:55 · 682 阅读 · 0 评论 -
【字符串】B038_LC_重新排列句子中的单词(模拟 / TreeMap)
一、ProblemGiven a sentence text (A sentence is a string of space-separated words) in the following format:First letter is in upper case.Each word in text are separated by a single space.Your task is to rearrange the words in text such that all words a原创 2020-05-17 14:59:22 · 227 阅读 · 0 评论 -
【字符串】C036_三位分节法(substring)
一、Problem输入第一行读入一个正整数n(允许存在前导0)。输出将整数n转换成以三位分节法表示001234001,234二、Solution方法一:暴力取子串import java.util.*;import java.math.*;import java.io.*;public class Main{ static class Solution { voi...原创 2020-05-04 15:50:53 · 2472 阅读 · 0 评论 -
【字符串】C035_分割字符串的最大得分(暴力计数 / 线性扫描)
一、ProblemGiven a string s of zeros and ones, return the maximum score after splitting the string into two non-empty substrings (i.e. left substring and right substring).The score after splitting a s...原创 2020-04-27 10:13:12 · 281 阅读 · 0 评论 -
【字符串】A001_统计重复个数(暴力 / 优化)
一、Problem由 n 个连接的字符串 s 组成字符串 S,记作 S = [s,n]。例如,[“abc”,3]=“abcabcabc”。如果我们可以从 s2 中删除某些字符使其变为 s1,则称字符串 s1 可以从字符串 s2 获得。例如,根据定义,“abc” 可以从 “abdbec” 获得,但不能从 “acbbe” 获得。现在给你两个非空字符串 s1 和 s2(每个最多 100 个字符长)...原创 2020-04-19 23:57:48 · 262 阅读 · 0 评论 -
【字符串】B034 数青蛙(字符计数)
一、题目描述给你一个字符串 croakOfFrogs,它表示不同青蛙发出的蛙鸣声(字符串 “croak” )的组合。由于同一时间可以有多只青蛙呱呱作响,所以 croakOfFrogs 中会混合多个 “croak” 。请你返回模拟字符串中所有蛙鸣所需不同青蛙的最少数目。注意:要想发出蛙鸣 “croak”,青蛙必须 依序 输出 ‘c’, ’r’, ’o’, ’a’, ’k’ 这 5 个字母。如果没...原创 2020-04-19 16:37:31 · 647 阅读 · 0 评论 -
【回溯】B032_长度为 n 的开心字符串中字典序第 k 小的字符串(回溯)
一、题目描述A happy string is a string that:consists only of letters of the set [‘a’, ‘b’, ‘c’].s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed).For example, strings “ab...原创 2020-04-19 10:26:16 · 358 阅读 · 0 评论 -
【字符串】B034 回文日期(枚举(未知错误) | 构造法)
一、题目描述二、题解方法一:枚举 + 细节边界问题:当输入样例为:1000010199991231预期:331输出:330问题出现在方法 valid 中,这样做不是对的吗?if (month == 2) { boolean isLeep = year % 4 == 0 && year % 100 != 0 || year%400 == 0; if (is...原创 2020-04-08 15:15:55 · 173 阅读 · 0 评论 -
【字符串 L】B033 单词分段(分段统计 | 记录高度差的个数)
一、题目描述小明对类似于 hello 这种单词非常感兴趣,这种单词可以正好分为四段,第一段由一个或多个辅音字母组成,第二段由一个或多个元音字母组成,第三段由一个或多个辅音字母组成,第四段由一个或多个元音字母组成。给定一个单词,请判断这个单词是否也是这种单词,如果是请输出 yes,否则请输出 no。元音字母包括 a, e, i, o, u,共五个,其他均为辅音字母。输入格式输入一行,包含...原创 2020-04-05 21:56:41 · 263 阅读 · 0 评论 -
【字符串】B032 构造 K 个回文字符串(利用性质)
一、题目描述给你一个字符串 s 和一个整数 k 。请你用 s 字符串中 所有字符 构造 k 个非空 回文串 。如果你可以用 s 中所有字符构造 k 个回文字符串,那么请你返回 True ,否则返回 False 。输入:s = "annabelle", k = 2输出:true解释:可以用 s 中所有字符构造 2 个回文字符串。一些可行的构造方案包括:"anna" + "elble","...原创 2020-04-05 20:06:00 · 244 阅读 · 0 评论 -
【位运算】B007_LC_将二进制表示减到 1 的步骤数(字符串二级制加法 / BigInteger)
一、题目描述给你一个以二进制形式表示的数字 s 。请你返回按下述规则将其减少到 1 所需要的步骤数:如果当前数字为偶数,则将其除以 2 。如果当前数字为奇数,则将其加上 1 。题目保证你总是可以按上述规则将测试用例变为 1 。输入:s = "1101"输出:6解释:"1101" 表示十进制数 13 。Step 1) 13 是奇数,加 1 得到 14 Step 2) 14 是偶...原创 2020-04-05 19:43:59 · 248 阅读 · 0 评论 -
【字符串】B031 自定义字符串排序(字符计数)
一、题目描述字符串 S 和 T 只包含小写字符。在 S 中,所有字符只会出现一次。S 已经根据某种规则进行了排序。我们要根据S中的字符顺序对T进行排序。更具体地说,如果S中x在y之前出现,那么返回的字符串中x也应出现在y之前。返回任意一种符合条件的字符串T。输入:S = "cba"T = "abcd"输出: "cbad"解释: S中出现了字符 "a", "b", "c", 所以 ...原创 2020-04-03 23:21:01 · 153 阅读 · 0 评论 -
【字符串】B030_「面试题」单词距离(暴力 | 绝对值距离)
一、题目描述You have a large text file containing words. Given any two words, find the shortest distance (in terms of number of words) between them in the file. If the operation will be repeated many times...原创 2020-04-03 22:46:50 · 286 阅读 · 0 评论 -
【字符串】C029_单词的压缩编码(字符计数(分类讨论))
一、题目描述Given two strings A and B of lowercase letters, return true if and only if we can swap two letters in A so that the result equals B.Input: A = "aaaaaaabc", B = "aaaaaaacb"Output: true二、题解方...原创 2020-04-01 23:21:44 · 211 阅读 · 0 评论 -
【字符串】B028_LC_有效括号的嵌套深度(树分层思想)
一、题目描述给你一个「有效括号字符串」 seq,请你将其分成两个不相交的有效括号字符串,A 和 B,并使这两个字符串的嵌套深度最小。嵌套深度:类似地,我们可以定义任意有效括号字符串 s 的 嵌套深度 depth(S):s 为空时,depth("") = 0s 为 A 与 B 连接时,depth(A + B) = max(depth(A), depth(B)),其中 A 和 B 都是有效...原创 2020-04-01 22:06:04 · 283 阅读 · 0 评论 -
【字符串】C028_最后一个单词的长度(split | 逆序遍历)
一、题目描述Given a string s consists of upper/lower-case alphabets and empty space characters ’ ', return the length of last word (last word means the last appearing word if we loop from left to right) in...原创 2020-03-29 10:17:10 · 241 阅读 · 0 评论 -
【字符串】C026_最常见的单词(map)
一、题目描述Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn’t banned, and that the...原创 2020-03-26 23:19:11 · 207 阅读 · 0 评论 -
【字符串】C029_面试题 左旋转字符串(旋转 n-k 次字符串 | substring)
一、题目描述字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。输入: s = "abcdefg", k = 2输出: "cdefgab"二、题解方法一:右旋 n-k 次字符串左旋 n 次,等于右旋 n-k 次。public Stri...原创 2020-03-26 22:22:33 · 298 阅读 · 0 评论 -
【字符串】C028_面试题 字符串轮转(旋转 n 次字符串 | 拼接 s2 | 字符计数)
一、题目描述二、题解方法一:旋转字符串每次旋转 s2 一次就和 s1 比较是否相等即可。效率不高,击败了 5% 的用户。public boolean isFlipedString(String s1, String s2) { if ("".equals(s1) && "".equals(s2) || s1.equals(s2)) re...原创 2020-03-26 22:11:44 · 261 阅读 · 0 评论 -
【字符串】C027_重复叠加字符串匹配(暴力匹配 | 双指针)
一、题目描述Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1.For example, with A = "abcd" and B = "cdabcdab...原创 2020-03-25 16:02:13 · 274 阅读 · 0 评论 -
【字符串】B025_验证回文字符串 II(暴力 | 双指针)
一、题目描述Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.Input: "abca"Output: TrueExplanation: You could delete the character 'c'.二、题解方...原创 2020-03-25 10:34:01 · 143 阅读 · 0 评论 -
【字符串】C007 验证回文串(Character 方法 | 造轮子)
一、题目给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。说明:本题中,我们将空字符串定义为有效的回文串。示例 1: 输入: "A man, a plan, a canal: Panama" 输出: true示例 2: 输入: "race a car" 输出: false二、分析 【1】思路设置左、右双指针 i,j,向中间判断;跳过 非...原创 2019-08-12 21:12:17 · 334 阅读 · 0 评论 -
【字符串】C024_LC_计数二进制子串(向前看同连续字符个数)
一、题目描述Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0’s and 1’s, and all the 0’s and all the 1’s in these substrings are grouped consecutively.S...原创 2020-03-24 11:40:53 · 273 阅读 · 0 评论 -
【字符串】C023_字符串中的第一个唯一字符(map | String 自带方法)
一、题目描述Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.s = "leetcode"return 0.s = "loveleetcode",return 2.二、题解方法一:mapmap 对字符...原创 2020-03-24 10:07:21 · 206 阅读 · 0 评论 -
【字符串】C022_长按键入串(双指针)
一、题目描述你的朋友正在使用键盘输入他的名字 name。偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次。你将会检查键盘输入的字符 typed。如果它对应的可能是你的朋友的名字(其中一些字符可能被长按),那么就返回 True。输入:name = "saeed", typed = "ssaaedd"输出:false解释:'e' 一定需要被键入两次,但在 typed...原创 2020-03-24 09:54:36 · 248 阅读 · 0 评论 -
【字符串】C021_根据二叉树创建字符串(递归)
一、题目描述You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.The null node needs to be represented by empty parenthesis pair “()”. An...原创 2020-03-23 14:59:22 · 209 阅读 · 0 评论