
字符串
文章平均质量分 78
jiyanfeng1
喜欢算法和编程的工科男
展开
-
找出一个字符串的所有anagram
给定一个字符串集合S和一个字符串str。要求设计一个数据结构,能够快速找出集合S里所有的、是str的anagram的字符串。思路:首先,要分析什么样的字符串才可以互称为anagram。两个字符串如果是anagram,它们经过排序后得到的两个字符串一定相等。或者,两个字符串如果是anagram,它们包含的字符的频率一定相等。所以,基于anagram的这两个性质,可以有以下两种数据结构方案原创 2012-10-02 13:07:24 · 3820 阅读 · 0 评论 -
[LeetCode] 把一个单词变形成为另一个单词,每次变形只改变一个单词 word ladder
给定两个相等长度的单词,写一个算法,把一个单词变形成为另一个单词,每次变形只改变一个单词。此过程中得到的每个单词必须是字典里存在的单词。EXAMPLEInput: DAMP, LIKEOutput: DAMP -> LAMP -> LIMP -> LIME -> LIKE思路:这其实就是广度优先遍历。遍历过程中,我们需要记录路径,在找到目标单词后,根据记录的路径打印出变形过原创 2012-10-16 10:34:32 · 3919 阅读 · 0 评论 -
压缩字符串
给你一个字符串,比如 “AAABBBCDEEFFF”,把这个字符串变成 "A3B3CDE2F3",达到压缩的目的。用in-place的方式进行压缩,代码如下:#include using namespace std;void compress(char* str){ char prev = *str; // used to decide whether *curr is an u原创 2013-02-21 10:18:42 · 594 阅读 · 0 评论 -
[LeetCode] Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without an原创 2015-01-10 21:56:07 · 555 阅读 · 0 评论 -
[LeetCode] Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover翻译 2014-12-23 17:45:12 · 612 阅读 · 0 评论 -
[LeetCode] Simplify Path
转自:http://fisherlei.blogspot.com/2013/01/leetcode-simplify-path.htmlGiven an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../..转载 2014-12-09 07:09:30 · 516 阅读 · 0 评论 -
左旋转字符串 left rotate string
左旋转字符串题目:字符串的左旋转操作:把字符串前面的若干字符移到字符串的后面。例如:字符串abcdefg左旋转2位得到cdefgab。请实现左旋转字符串函数,要求对于长度为n的字符串,时间复杂度为O(n),辅助内存为O(1)。算法思想:可以利用三次字符串反转操作,来达到左旋转字符串的目的:1.反转字符串的前半段;2.反转字符串的后半段;3.反转整个字符串。原创 2012-08-24 04:46:21 · 5719 阅读 · 0 评论 -
[LeetCode] Mutiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative.用逐位相乘的方法,复杂度是O(m+n),m和n分别是 str1 和 str2 的长度原创 2014-12-26 15:19:09 · 601 阅读 · 0 评论 -
Find common characters in a set of strings
Find the number of letters common in a given set of strings. Note that the given set of strings might have duplicates.思路例如给定如下3个字符串:aaaabbchdjnffbccgdaabstep 1:对每个字符串都去重处理。step 2:用原创 2015-01-17 00:43:55 · 622 阅读 · 0 评论 -
[LeetCode] minimum window 包含所有字符的最小子字符串
包含所有字符的最小子字符串 smallest substring which contains all characters from a given stringSuppose you are given following:原创 2014-09-22 05:36:41 · 886 阅读 · 0 评论 -
[LeetCode] Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very lar原创 2015-01-15 00:57:19 · 1088 阅读 · 0 评论 -
后缀树和前缀树
给你一个很长的字符串 s,和一堆短字符串 T = {t1, t2, ...}。 设计一个高效的算法,确定T 里的每个字符串是不是 s 的子串。思路:先对字符串s构建一个后缀树,然后看看t1,t2...,是不是s的一个后缀的前缀。构建后缀树和查找后缀树的前缀都可以用递归的思路来解决。具体代码如下:public class SuffixTree { SuffixTreeNode ro原创 2015-07-31 11:12:05 · 1005 阅读 · 0 评论 -
字符串旋转-回文字符串
相关问题:最长回文子串给你一个字符串,求问,这个字符串旋转之后是不是一个回文字符串?例如:给你字符串 str = "aaaab",那么str旋转之后可以得到 str_new = "aabaa",是个回文字符串。思路:可以把str和str接起来,得到“aaaabaaaab”,然后对"aaaabaaaab"求最长回文字符子串。利用Machester算法,复杂度是O(n).原创 2015-08-02 03:29:44 · 713 阅读 · 0 评论 -
[LeetCode] Shortest Palindrome I
相关问题1:最长回文子串相关问题2:Minimum insertions to form a palindromeGiven a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest pal原创 2015-08-03 11:13:52 · 933 阅读 · 0 评论 -
[LeetCode] distinct subsequence
Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be non原创 2014-12-05 04:43:54 · 802 阅读 · 0 评论 -
[LeetCode] Reverse Words in a String
Given an input string, reverse the string word by word.For example, Given s = "the sky is blue", return "blue is sky the".Clarification:What constitutes a word?A sequence of non-space ch原创 2015-01-10 22:28:57 · 712 阅读 · 0 评论 -
有n 个长为m+1 的字符串,求前后m个字符匹配所能形成的最长字符串链
有n个长为m+1的字符串,如果某个字符串的最后m个字符与某个字符串的前m个字符匹配,则两个字符串可以联接,问这n个字符串最多可以连成一个多长的字符串,如果出现循环,则返回错误。可以用hash table来做。大致思路如下:初始化两个hash表,一个是前缀hashtable,一个是后缀hashtable。前缀hashtable的键为字符串的前m个字符,值为字符串最后一个字符。后缀hashta原创 2012-09-21 12:56:18 · 1739 阅读 · 0 评论 -
最长不重复子串的长度&最长重复子串
求给定的某一个字符串中的最长不重复子串的长度。例如字符串s为:“abcdefgegcsgcasse”,其最长的不重复子串为“abcdefg”,长度为7最长不重复子串的解法一: 设置一个辅助数据结构(如map)记录每个字符最后一次出现的位置;遍历字符串中的每个字符,如果在map中没有出现,则不重复子串的长度+1,并更新最大字符串的长度值; 如果在map中已原创 2012-10-12 14:35:53 · 4290 阅读 · 0 评论 -
字符串的排列组合问题
本文转自:http://blog.youkuaiyun.com/wuzhekai1985/article/details/6643127 问题1 :输入一个字符串,打印出该字符串中字符的所有排列。例如输入字符串abc,则输出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba。 思路:这是个递归求解的问题。递归算法有四个特性:(1)必须转载 2012-10-01 14:02:26 · 2112 阅读 · 0 评论 -
用户的输入常常会有错误,错误主要分三种情况,多一个,少一个,写错一个。
海量数据查询问题:用户的输入常常会有错误,错误主要分三种情况,多一个,少一个,写错一个。假如我们在数据库中存储了所有正确的输入,利用什么技术,例如输入一个错误query,来得到所有正确的query。这个题目是hr提示我的,其实我也没有什么思路想到的是hash表对正确的数据存储的时候做如下处理:例如我们要存储abcdef,首先存储abcdef状转载 2012-10-19 09:44:20 · 1625 阅读 · 0 评论 -
最长对称字符串问题/最长回文子串问题
题目:输入一个字符串,输出该字符串中对称的子字符串的最大长度。比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4。何海涛日志中给出算法是:先判断子字符串A是不是对称的。如果A不是对称的,那么向该子字符串两端各延长一个字符得到的字符串肯定不是对称的。如果A对称,那么我们只需要判断A两端延长的一个字符是不是相等的,如果相等,则延长后的字符串是对称的。因此在知原创 2012-10-12 13:20:50 · 4241 阅读 · 0 评论 -
最长公子序列 Longest Common Subsequence
设 X = (x0, x1, ... , x_(n-1) ), Y = (y0, y1, ... , y_(m-1) ), 那么用动态规划的方法,可以得到如下的阶段决策方程:代码如下:#include#include#include#include using namespace std;int LCSubsequence(char* s1, char* s2){ if原创 2013-02-26 02:08:11 · 926 阅读 · 0 评论 -
百度笔试题目剖析——英文拼写纠错
题目: 在用户输入英文单词时,经常发生错误,我们需要对其进行纠错。假设已经有一个包含了正确英文单词的词典,请你设计一个拼写纠错的程序。(1)请描述你解决这个问题的思路;(2)请给出主要的处理流程,算法,以及算法的复杂度;(3)请描述可能的改进(改进的方向如效果,性能等等,这是一个开放问题)。 网上流传解答: (1)思路:字典以字母键树组织,在用户输入同时匹配转载 2014-09-18 23:35:20 · 7076 阅读 · 0 评论 -
实现memmove函数
用C语言实现函数void * memmove(void *dest,const void *src,size_t n)。memmove函数的功能是拷贝src所指的内存内容前n个字节到dest所指的地址上。注意:memmove和么么从哦原创 2014-09-18 22:56:18 · 1053 阅读 · 0 评论 -
字符串相关处理kmp,前缀数,后缀树,后缀数组,最长回文串,最长重复字串,最长非重复字串
1. 最长回文串一般用后缀数组或者后缀树可以解决,用此方法:http://blog.youkuaiyun.com/v_july_v/article/details/6897097预处理后缀树,使得查询LCA的复杂度为O(1)。这步的开销是O(N),N是单词S的长度 ;对单词的每一位置i(也就是从0到N-1),获取LCA(S(i), S‘(N-i-1)) 以及LCA(S(i),转载 2012-10-07 12:52:29 · 2440 阅读 · 0 评论 -
最长的无重复字符的字符串 longest substring with no repeating characters
最长的无重复字符的字符串 longest substring with no repeating characters原创 2014-11-21 12:35:47 · 725 阅读 · 0 评论 -
[LeetCode] Palindrome Partitioning 回文分割
Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.For example, given s = "aab", Return [ ["aa","b"],原创 2014-12-03 09:11:06 · 670 阅读 · 0 评论 -
反转字符串
用C语言实现一个revert函数,它的功能是将输入的字符串在原串上倒序后返回.#include void reverse(char s[]){ int length = strlen(s) ; int c, i, j; for (i = 0, j = length - 1; i < j; i++, j--) { c原创 2014-09-18 22:08:13 · 649 阅读 · 0 评论 -
最长回文子串
Longest Palindromic Substring. Here, we describe an algorithm (Manacher’s algorithm) which finds the longest palindromic substring in linear time. Please read Part I for more background informatio翻译 2014-09-20 05:10:38 · 1270 阅读 · 0 评论