import java.util.Scanner; // 1:无需package // 2: 类名必须Main, 不可修改 public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); //在此输入您的代码...
Description You are given an array of words where each word consists of lowercase English letters. wordA is a predecessor of wordB if and only if we can insert exactly one letter anywhere in wordA without changing the order of the other characters to mak
原题传送门 1. 题目描述 2. Solution 1 1、思路分析 I. 状态定义 f[i][j] = s的前i个字符与p中的前j个字符是否能够匹配。 II. 初始状态 f[0][0] = true,即两个空字符串是可以匹配的。 III. 状态转移方程 对于匹配情况的讨论 若p[j]是小写字母,则必须在s中匹配一个相同的字
原题传送门 1. 题目描述 2. Solution 1 1、思路分析 自动状态机 a.) 输入字符所有可能情况分类: space(空格), +/-, number, other b.) 根据当前状态(字符),与下一个字符可以得出下一状态: 即 下一状态 = f(当前状态, 下一字符) 下一字符 space +/- number
不含重复字符的最长子串: 代码实现 public int longestSubStr(String str) { int max = 0; Deque<Character> deque = new ArrayDeque<>(); for (int i = 0; i < str.length(); i++) { if (!deque.contains(str.charAt(i))) {
题目 给定两个字符串 a 和 b,寻找重复叠加字符串 a 的最小次数,使得字符串 b 成为叠加后的字符串 a 的子串,如果不存在则返回 -1。 注意:字符串 "abc" 重复叠加 0 次是 "",重复叠加 1 次是 "abc",重复叠加 2 次是 "abcabc"。 解答 package Com.Xu.String; public class SixEightSix
package com.zou.Algorithm.kmp;import java.util.Arrays;public class KmpAlgorithm { public static void main(String[] args) { String str1="BBC ABCDAB ABCDABCDABDE"; String str2="ABCDABD"; int[] next=kmpNext("A
KMP的主要思想是当出现字符串不匹配时,可以知道一部分之前以及匹配的文本内容,可以利用这些信息避免从头再去做匹配。如何记录已经匹配的文本内容,是KMP的重点,也是next数组肩负的重任。 这个next数组为前缀表,代表的是模式串中当前位置及其之前的子串相同前后缀的长度的最大值。(这里
class Solution { public boolean isAnagram(String s, String t) { if (s.length() != t.length()) { return false; } int[] table = new int[26]; for (int i = 0; i < s.length(); i++) { table[s.charAt
1 public static void isPalindrome() { 2 int a = 1122111; 3 //Integer integer = new Integer(a); 4 //自动装箱 5 Integer integer = a; 6 String str = integer.toString(); 7 int strLength = str.length(); 8
题目 给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 ‘.’ 和 ‘’ 的正则表达式匹配。 ‘.’ 匹配任意单个字符 '’ 匹配零个或多个前面的那一个元素 示例 输入:s = “aa” p = “a” 输出:false 解释:“a” 无法匹配 “aa” 整个字符串。 算法 dp[i][j]下标从1到
class Solution { public List<String> removeInvalidParentheses(String s) { List<String> ans = new ArrayList<String>(); Set<String> currSet = new HashSet<String>(); currSet.add(s); while (tr
import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; /* * 回文时间 */ public class Main { public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub Scanner in = new Scanner(System.in);
很久之前做的答案 public boolean isValid(String s) { char[] c = new char[10000]; int j=0; for(int i=0;i<s.length();i++){ switch(c[j]){ case '(': if(s.charAt(i)==')') j--
KMP 什么是 KMP 算法?next 数组题录28. 实现 strStr()459. 重复的子字符串 刷题路线以及 KMP 详解 :代码随想录 什么是 KMP 算法? KMP 是以 3 位发明者名字的首字母命名的,常用来字符串匹配上。如:判断 s1 是不是 s2 的子串,KMP 算法将解决此问题的时间复杂度从 O(m*n) 降低
This is two points problem, just concentrate and carefully deal with the characters, then the problem can be solved. This is a very good problem for writing all kinds of edge test cases. public boolean validWordAbbreviation(String word, String abbr)
This is the typical DP problem, the time complexity is O(n2), Where n is the length of string s. This is due to the fact that we try to find result for all combinations of l and r where l and r range from 0 to n: private int[][] visited; p
1.题目 2.条件与思路 利用双指针 3.解题过程 class Solution { public int lengthOfLongestSubstring(String s) { int len = s.length(); boolean flag = true; List l = new ArrayList(); for(int i=0;i<len;i++){ char
2047. 句子中的有效单词数 Solution 思路:简单模拟题,首先将单词划分开,然后根据条件一个一个否定即可。标记连接符和标点符号的位置,进行判断,这里注意特判是在数值为1的时候做的。 class Solution { public int countValidWords(String sentence) { String[] words = sen
原题在这里: https://leetcode.com/discuss/interview-question/1273766/longest-common-substring 这道题跟https://www.cnblogs.com/feiflytech/p/15841611.html 类似,但是不完全相同: 首选自顶向下写个递归,与1143只有一点点不同。 private Integer[][] dp; private int m
题目1: 拼写单词 解题思路: 使用哈希表存储chars中每个字母的数量 再使用一个哈希表存储word中每个字母的数量 将这两个哈希表的键值对逐一进行比较 //先来一个比较通俗易懂的代码 class Solution{ public int countCharacters(String[] words, String chars){
本文是每天跟着代码随想录刷题时做的笔记,用来总结与复习。 目录 344.反转字符串 541.反转字符串Ⅱ 剑指offer 05.替换空格 151.反转字符串里的单词 剑指offer 58-Ⅱ.左旋转字符串 28.实现strStr() 459.重复的子字符串 今日总结 344.反转字符串 题目链接:344. 反转字符串 - 力
String类中的public char charAt(int index)方法 返回索引处的char值 使用for循环遍历输出字符串 package Experience; import java.util.Scanner; public class iterateString { public static void main(String args[]){ Scanner str=new Scanner(System.in);
1332. 删除回文子序列 Solution 思路: 回文子序列 所以最多两次,如果一开始就是回文串的话 就是一次。 class Solution { public int removePalindromeSub(String s) { int i =0, j = s.length() - 1; while (i < j) { if (s.charAt(i) == s.charAt
请实现一个函数用来匹配包含'. '和''的正则表达式。模式中的字符'.'表示任意一个字符,而''表示它前面的字符可以出现任意次(含0次)。在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"abaca"匹配,但与"aa.a"和"ab*a"均不匹配。 来源:力扣(LeetCode) 链接:ht