substring

Go test string contains substring

旧街凉风 提交于 2020-03-12 14:06:35
问题 How do I check if a string is a substring of another string in Go? For example, I want to check someString.contains("something") . 回答1: Use the function Contains from the strings package. import { "strings" } strings.Contains("something", "some") // true 来源: https://stackoverflow.com/questions/45266784/go-test-string-contains-substring

1371. Find the Longest Substring Containing Vowels in Even Counts

早过忘川 提交于 2020-03-09 14:37:55
如果按照正常思路,估计要o(N*N)的复杂度,采用累加法,只用O(N)的复杂度,只有这样才能通过 class Solution { public: // 累加法,直接o1复杂度,但是存储空间会变大 //存储aeiou 2^5=32种情况的所有index int convert(map<int, int>& cnt){ int a = cnt['a']%2*pow(2, 0); int e = cnt['e']%2*pow(2, 1); int i = cnt['i']%2*pow(2, 2); int o = cnt['o']%2*pow(2, 3); int u = cnt['u']%2*pow(2, 4); return (a+e+i+o+u); } int findTheLongestSubstring(string s) { vector<int> temp; vector<vector<int>> res(32, temp); res[0].push_back(-1); map<int,int> cnt; for(int i=0;i<s.size();i++){ if (cnt.find(s[i])==cnt.end()){ cnt[s[i]] = 1; } else{ cnt[s[i]] ++; } res[convert(cnt)].push_back(i); }

3. Longest Substring Without Repeating Characters

三世轮回 提交于 2020-03-07 17:18:19
/** * 3. Longest Substring Without Repeating Characters * https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ * https://www.youtube.com/watch?v=3IETreEybaA * * Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: Input: "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Note that the answer must be a

Get all possible substring in php

本小妞迷上赌 提交于 2020-03-06 03:18:12
问题 I am trying to get a list of all substring of input. for input=a, substrings {'','a'} for input=ab, substrings {'','a','b','ab','ba'} for input=abc, substrings {'','a','b','c','ab','bc','ca','ba','cb','ac','abc','acb','bac','bca','cab','cba'} and so on. The code I tried is here function get_substr($string){ $array=str_split($string); static $k=0; for ($i=0; $i <count($array) ; $i++) { for ($j=0; $j <count($array) ; $j++) { $new_array[$k]=substr($string, $i, $j - $i + 1); $k++; } } return($new

Java-截取字符串substring() 方法

风格不统一 提交于 2020-03-04 11:12:02
substring() 方法返回字符串的子字符串。 1. public String substring(int beginIndex) 返回一个新字符串,它是此字符串的一个子字符串。该子字符串始于指定索引处的字符,一直到此字符串末尾。 参数:beginIndex - 开始处的索引(包括), 返回:指定的子字符串, 异常:如果 beginIndex 为负或大于此 String 对象的长度,则抛出IndexOutOfBoundsException str = abcdEF ; str.substring ( 3 ) ; 为dEF 2. public String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始, endIndex:到指定的 endIndex-1处结束。 参数: beginIndex - 开始处的索引(包括),从0开始。 endindex --结尾处索引(不包括)。 返回:指定的子字符串。 抛出:如果 beginIndex 为负,或length大于字符串长度,则抛出IndexOutOfBoundsException str = abcdEF ; str.substring ( 3,5 ) ; 为dE 来源: CSDN 作者: qq

动态规划-TSP问题-最短超级串

为君一笑 提交于 2020-03-04 00:35:39
2020-03-03 22:55:08 问题描述: 给定一个字符串数组 A,找到以 A 中每个字符串作为子字符串的最短字符串。 我们可以假设 A 中没有字符串是 A 中另一个字符串的子字符串。 示例 1: 输入:["alex","loves","leetcode"] 输出:"alexlovesleetcode" 解释:"alex","loves","leetcode" 的所有排列都会被接受。 示例 2: 输入:["catg","ctaagt","gcta","ttca","atgcatc"] 输出:"gctaagttcatgcatc" 提示: 1 <= A.length <= 12 1 <= A[i].length <= 20 问题求解: 解法一 :暴力求解 首先我们要明确的就是,本题可以转化成图论的题目,就是在一个图中要遍历所有的节点一次,最后路径的最小值是多少。(这里和TSP略有不同,即我们不需要返回起始节点) 暴力求解,可以理解为全排列,只不过我们做了一些剪枝操作进行了加速。 时间复杂度:O(n!) int res = (int)1e9; List<Integer> path; int n; public String shortestSuperstring(String[] A) { n = A.length; int[][] graph = new int[n][n];

leetcode 459. Repeated Substring Pattern

╄→гoц情女王★ 提交于 2020-03-03 14:37:30
方法一:采用kmp算法,获得next数组。并且next数组的最后一个元素代表的长度一定是s.length()-next[-1]的整数倍。 class Solution { public boolean repeatedSubstringPattern ( String s ) { int [ ] kmp = getnext ( s ) ; int len = s . length ( ) ; return ( kmp [ len ] != 0 ) && ( kmp [ len ] % ( len - kmp [ len ] ) == 0 ) ; } public int [ ] getnext ( String s ) { if ( s . length ( ) == 1 ) { return new int [ ] { - 1 , 0 } ; } int [ ] next = new int [ s . length ( ) + 1 ] ; next [ 0 ] = - 1 ; next [ 1 ] = 0 ; int cn = 0 ; //最长前缀的长度 int i = 2 ; while ( i < next . length ) { if ( s . charAt ( i - 1 ) == s . charAt ( cn ) ) { next [ i ++ ] = ++

CF1234F Yet Another Substring Reverse (状压dp)

牧云@^-^@ 提交于 2020-03-03 11:19:12
首先我们发现,因为可以在任意地方翻转,所以最后的答案就是一个合法子串和他的补集的子集中个数和最大的那个 因此我们先枚举每一个合法状态,记录他的合法个数有几个。 然后我们从头枚举每一个状态,计算状态的子集中的最大个数。 这样我们最后只要枚举状态和补集,就能计算出真正的答案了 #include<iostream> #include<vector> #include<cstdio> #include<string> #include<cstring> #include<algorithm> using namespace std; const int N=21; int f[1<<21]; int n; string s; int st[N]; int main(){ cin>>s; int i,j; for(i=0;i<s.size();i++){ int tmp=0; int cnt=0; memset(st,0,sizeof st); for(j=i;j<s.size();j++){ //枚举每一个合法状态 if(st[s[j]-'a']) break; tmp+=1<<(s[j]-'a'); st[s[j]-'a']=1; cnt++; f[tmp]=cnt; } } for(i=0;i<1<<20;i++){ //dp思想由子集的最大数来判断当前状态的最大个数 for(j=0

[LeetCode] 159. Longest Substring with At Most Two Distinct Characters

ぃ、小莉子 提交于 2020-02-28 13:13:56
最多有两个不同字符的最长子串。题意是给一个字符串,请返回一个最长子串的长度。子串的要求是最多只有两个不同的字母。例子, Example 1: Input: "eceba" Output: 3 Explanation: tis "ece" which its length is 3. Example 2: Input: "ccaabbb" Output: 5 Explanation: tis "aabbb" which its length is 5. 这题依然是用到sliding window的思想。给左右两个指针,并且创建一个hashmap,key是遍历到的字母,value是字母最后一次出现位置的index。扫描的时候,一开始只让右指针移动,把遍历到的字母和当前的坐标值加入hashmap。此时最大子串的长度就是左右指针的间距。当右指针遍历到第三个不同字母的时候,需要扫描hashmap,找到value(记为leftmost)最小的那个key并且删除这个键值对。此时左指针的位置是leftmost + 1。 时间O(n) 空间O(1) - 虽然有hashmap但是size很小,只有26个字母 1 /** 2 * @param {string} s 3 * @return {number} 4 */ 5 var lengthOfLongestSubstringTwoDistinct =

leetcode76 Minimum Window Substring

陌路散爱 提交于 2020-02-27 00:22:22
1 """ 2 Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). 3 Example: 4 Input: S = "ADOBECODEBANC", T = "ABC" 5 Output: "BANC" 6 """ 7 """ 8 题很难,debug一遍才看明白。debug过程如下: 9 初始化:count{A:1, B:1, C:1} cnt = 0 滑动窗口[] 10 1. {A:0, B:1, C:1} cnt = 1 [A] 11 2. {A:0, B:1, C:1, D:-1} cnt = 1 [AD] 12 3. {A:0, B:1, C:1, D:-1, O:-1} cnt = 1 [ADO] 13 4. {A:0, B:0, C:1, D:-1, O:-1} cnt = 2 [ADOB] 14 5. {A:0, B:0, C:1, D:-1, O:-1, E:-1} cnt = 2 [ADOBE] 15 6. {A:0, B:0, C:0, D:-1, O:-1, E:-1} cnt = 3 [ADOBEC] 16 ----------------cnt==len(t