[关闭]
@tankle 2015-05-13T01:53:51.000000Z 字数 5131 阅读 3493

LintCode 进阶题解 之 Stirng

LintCode


Two Strings Are Anagrams

Write a method anagram(s,t) to decide if two strings are anagrams or not.

Example
Given s="abcd", t="dcab", return true.

Challenge
O(n) time, O(1) extra space

  1. class Solution {
  2. public:
  3. /**
  4. * @param s: The first string
  5. * @param b: The second string
  6. * @return true or false
  7. */
  8. bool anagram(string s, string t) {
  9. // write your code here
  10. int m = s.size(), n = t.size();
  11. if(m != n)
  12. return false;
  13. int sum = 0;
  14. for(int i=0; i<m; i++){
  15. sum += s[i] - 'a';
  16. sum -= t[i] - 'a';
  17. }
  18. return sum == 0;
  19. }
  20. };

Compare Strings

26% Accepted
Compare two strings A and B, determine whether A contains all of the characters in B.

The characters in string A and B are all Upper Case letters.

Example
For A = "ABCD", B = "ABC", return true.

For A = "ABCD", B = "AABC", return false.

  1. class Solution {
  2. public:
  3. /**
  4. * @param A: A string includes Upper Case letters
  5. * @param B: A string includes Upper Case letter
  6. * @return: if string A contains all of the characters in B return true
  7. * else return false
  8. */
  9. bool compareStrings(string A, string B) {
  10. // write your code here
  11. int m = A.size(), n = B.size();
  12. if(0 == n)
  13. return true;
  14. if(m < n)
  15. return false;
  16. vector<int> numa(26,0);
  17. vector<int> numb(26, 0);
  18. for(int i=0; i<m; i++)
  19. numa[A[i] - 'A']++;
  20. for(int i=0; i<n; i++)
  21. numb[B[i] - 'A']++;
  22. for(int i=0; i<26; i++)
  23. if(numa[i] < numb[i])
  24. return false;
  25. return true;
  26. }
  27. };

strStr

18% Accepted
strstr (a.k.a find sub string), is a useful function in string operation. You task is to implement this function.

For a given source string and a target string, you should output the "first" index(from 0) of target string in source string.

If target is not exist in source, just return -1.

Example
If source="source" and target="target", return -1.

If source="abcdabcdefg" and target="bcd", return 1.

Challenge
O(n) time.

Clarification
Do I need to implement KMP Algorithm in an interview?
- Not necessary. When this problem occurs in an interview, the interviewer just want to test your basic implementation ability.

  1. class Solution {
  2. public:
  3. /**
  4. * Returns a index to the first occurrence of target in source,
  5. * or -1 if target is not part of source.
  6. * @param source string to be scanned.
  7. * @param target string containing the sequence of characters to match.
  8. */
  9. int strStr(const char *source, const char *target) {
  10. // write your code here
  11. if(source == NULL or target == NULL)
  12. return -1;
  13. int m = strlen(source), n = strlen(target);
  14. if(m < n)
  15. return -1;
  16. if( m == 0 && n == 0)
  17. return 0;
  18. int result = -1;
  19. for(int i=0; i<m; i++){
  20. int k = i;
  21. int j;
  22. for(j=0; j<n && k < m; ++k, ++j){
  23. if(source[k] != target[j])
  24. break;
  25. }
  26. if(j >=n ){
  27. result = i;
  28. break;
  29. }
  30. }
  31. return result;
  32. }
  33. };

Anagrams

24% Accepted
Given an array of strings, return all groups of strings that are anagrams.

Example
Given ["lint", "intl", "inlt", "code"], return ["lint", "inlt", "intl"].

Given ["ab", "ba", "cd", "dc", "e"], return ["ab", "ba", "cd", "dc"].

Note
All inputs will be in lower-case

  1. class Solution {
  2. public:
  3. /**
  4. * @param strs: A list of strings
  5. * @return: A list of strings
  6. */
  7. vector<string> anagrams(vector<string> &strs) {
  8. // write your code here
  9. int m = strs.size();
  10. vector<string> newstr;
  11. for(int i=0; i<m; ++i){
  12. string tmp = strs[i];
  13. sort(tmp.begin(), tmp.end());
  14. //cout<<tmp<<endl;
  15. newstr.push_back(tmp);
  16. }
  17. map<string, int> hashstr;
  18. for(int i=0; i<m; ++i){
  19. if(hashstr.find(newstr[i]) == hashstr.end())
  20. hashstr[newstr[i]] = 0;
  21. hashstr[newstr[i]] += 1;
  22. }
  23. vector<string> result;
  24. for(int i=0; i<m; ++i){
  25. if(hashstr[newstr[i]] > 1)
  26. result.push_back(strs[i]);
  27. }
  28. return result;
  29. }
  30. };

Longest Common Substring

35% Accepted
Given two strings, find the longest common substring.

Return the length of it.

Example
Given A="ABCD", B="CBCE", return 2.

Note
The characters in substring should occur continuously in original string. This is different with subsequence.

  1. class Solution {
  2. public:
  3. /**
  4. * @param A, B: Two string.
  5. * @return: the length of the longest common substring.
  6. */
  7. int longestCommonSubstring(string &A, string &B) {
  8. // write your code here
  9. int m = A.size(), n = B.size();
  10. vector<vector<int>> dp(m, vector<int>(n));
  11. int result = 0;
  12. for(int i=0; i<m; ++i){
  13. for(int j=0; j<n; ++j){
  14. if(0 == i or 0 == j){
  15. dp[i][j] = A[i] == B[j] ? 1:0;
  16. }
  17. else{
  18. if(A[i] == B[j])
  19. dp[i][j] = dp[i-1][j-1] + 1;
  20. else
  21. dp[i][j] = 0;
  22. }
  23. result = max(result, dp[i][j]);
  24. }
  25. }
  26. return result;
  27. }
  28. };

Longest Common Prefix

31% Accepted
Given k strings, find the longest common prefix (LCP).

Example
For strings "ABCD", "ABEF" and "ACEF", the LCP is "A"

For strings "ABCDEFG", "ABCEFG" and "ABCEFA", the LCP is "ABC"

  1. class Solution {
  2. public:
  3. /**
  4. * @param strs: A list of strings
  5. * @return: The longest common prefix
  6. */
  7. string LCP(string one, string two){
  8. int m = one.size(), n = two.size();
  9. string prefix = "";
  10. int len = min(m,n);
  11. for(int i=0; i<len; i++){
  12. if(one[i] == two[i])
  13. prefix += one[i];
  14. else
  15. break;
  16. }
  17. return prefix;
  18. }
  19. string longestCommonPrefix(vector<string> &strs) {
  20. // write your code here
  21. int m = strs.size();
  22. if(m == 0)
  23. return "";
  24. if(1 == m)
  25. return strs[0];
  26. string comstr = LCP(strs[0], strs[1]);
  27. for(int i=2; i<m; ++i){
  28. comstr = LCP(comstr, strs[i]);
  29. }
  30. return comstr;
  31. }
  32. };

String to Integer(atoi)

14% Accepted
Implement function atoi to convert a string to an integer.

If no valid conversion could be performed, a zero value is returned.

If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

Example
"10" => 10

"-1" => -1

"123123123123123" => 2147483647

"1.0" => 1

  1. class Solution {
  2. public:
  3. /**
  4. * @param str: A string
  5. * @return An integer
  6. */
  7. int atoi(string str) {
  8. // write your code here
  9. int result = 0;
  10. int m = str.size();
  11. int i = 0;
  12. while(str[i]== ' ') ++i;
  13. int sign = 1;
  14. if(str[i] == '-'){
  15. ++i;
  16. sign = -1;
  17. }
  18. else if(str[i] == '+'){
  19. ++i;
  20. sign = 1;
  21. }
  22. for(; i <m; ++i){
  23. if(str[i] < '0' || str[i] > '9')
  24. break;
  25. if((result > INT_MAX / 10) ||
  26. (result == INT_MAX/10
  27. && str[i] - '0' > INT_MAX %10))
  28. return sign == -1 ? INT_MIN : INT_MAX;
  29. result = result*10 + str[i] - '0';
  30. }
  31. return sign == 1 ? result : -1*result;
  32. }
  33. };
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注