码迷,mamicode.com
首页 > 其他好文 > 详细

1字符串

时间:2018-01-13 11:21:41      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:false   排序   div   put   sar   art   fga   rest   stat   

  strStr
Problem Statement
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 does 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(n2) is acceptable. Can you implement an O(n) algorithm? (hint: KMP)

Clarification
Do I need to implement KMP Algorithm in a real interview?
Not necessary. When you meet this problem in a real interview, the interviewer may just want to test your basic implementation ability. But make sure your confirm with the interviewer first.

//C++
class Solution{
public:
    int strStr(string haystack, string needle){
        if(haystack.empty() && needle.empty()) return 0;
        if(haystack.empty()) return -1;
        if(needle.empty()) return 0;
        if(haystack.size() < needle.size()) return -1;
        
        for(int i = 0; i < haystack.size() - needle.size() + 1; i++){
            string::size_type j = 0;
            for(; j < needle.size(); j++){
                if(haystack[i+j] != needle[j]) break;
            }
            if(j == needle.size()) return i;
        }
        return -1;
    }
};

 


  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 - hashmap 统计字频
class Solution{
public:
    bool anagram(string s, string t){
        if(s.empty() || t.empty()){
            return false;
        }
        if(s.size() != t.size()){
            return false;
        }
        int letterCount[256] = {0};
        for(int i = 0; i != s.size(); i++){
            ++letterCount[s[i]];
            --letterCount[t[i]];
        }
        for(int i = 0; i != t.size(); i++){
            if(letterCount[t[i]] != 0){
                return false;
            }
        }
        return true;
    }
};
//题解2 - 排序字符串
class Solution{
public:
    bool anagram(string s, string t){
        if(s.empty() || t.empty()){
            return false;
        }
        if(s.size() != t.size()){
            return false;
        }
        sort(s.begin(), s.end());
        sort(t.begin(), t.end());
        if(s == t){
            return true;
        }else{
            return false;
        }
    }
};

 

 


  Compare Strings
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.

//题目意思是问B中的所有字符是否都在A中,而不是单个字符。比如B="AABC"包含两个「A」,而A="ABCD"只包含一个「A」,故返回false. 做题时注意题意,必要时可向面试官确认。
class Solution{
public:
    bool compareStrings(string A, string B){
        if(A.size() < B.size()){
            return false;
        }
        const int AlphabetNum = 26;
        int letterCount[AlphabetNum] = {0};
        for(int i = 0; i != A.size(); ++i){
            ++letterCount[A[i] - A];
        }
        for(int i = 0; i != B.size(); i++){
            --letterCount[B[i] - A];
            if(letterCount[B[i] - A] < 0){
                return false;
            }
        }
        return true;
    }
};

 

 

  


  Anagrams
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 - 双重for循环(TLE)
class Solution{
public:
    vector<string> anagrams(vector<string> &strs){
        if(strs.size() < 2){
            return strs;
        }
        vector<string> result;
        vector<bool> visited(strs.size(), false); 
        for(int s1 = 0; s1 != strs.size(); ++s1){
            bool has_anagrams = false;
            for(int s2 = s1 + 1; s2 < strs.size(); ++s2){
                if((!visited[s2]) && isAnagrams(strs[s1], strs[s2])){
                    result.push_back(strs[s2]);
                    visited[s2] = true;
                    has_anagrams = true;
                }
            }
            if((!visited[s1]) && has_anagrams) result.push_back(strs[s1]);
        }
        return result;
    }
private:
    bool isAnagrams(string &s, string &t){
        if(s.size() != t.size()){
            return false;
        }
        const int AlphabetNum = 26;
        int letterCount[AlphabetNum] = {0};
        for(int i = 0; i != s.size(); ++i){
            ++letterCount[s[i] - a];
            --letterCount[t[i] - a];
        }
        for(int i = 0; i != t.size(); i++){
            if(letterCount[t[i] - a] < 0){
                return false;
            }
        }
        return true;
    }
};
//题解2 - 排序 + hashmap
class Solition{
public:
    vector<string> anagrams(vector<string> &strs){
        unordered_map<string, int> hash;
        for(int i = 0; i < strs.size(); i++){
            string str = strs[i];
            sort(str.begin(), str.end());
            ++hash[str];
        }
        
        vector<string> result;
        for(int i = 0; i < strs.size(); i++){
            string str = strs[i];
            sort(str.begin(), str.end());
            if(hash[str] > 1){
                result.push_back(strs[i]);
            }
        }
        return result;
    }
};

 

 

 

  


  Longest Common Substring
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.

class Solution{
public:
    int longestCommonSubstring(string &A, string &B){
        if(A.empty() || B.empty()){
            return 0;
        }
        int lcs = 0, lcs_temp = 0;
        for(int i = 0; i < A.size(); ++i){
            for(int j = 0; j <B.size(); j++){
                lcs_temp = 0;
                while((i + lcs_temp < A.size()) && (j + lcs_temp < B.size()) && (A[i + lcs_temp] == B[j + lcs_temp])){
                    ++lcs_temp;
                }
                if(lcs_temp > lcs){
                    lcs = lcs_temp;
                }
            } 
        }
        return lcs;
    }
};

 


  Rotate String
Problem Statement
Given a string and an offset, rotate string by offset. (rotate from left to right)
Example
Given "abcdefg".
offset=0 => "abcdefg"
offset=1 => "gabcdef"
offset=2 => "fgabcde"
offset=3 => "efgabcd"

 

class Solution{
public:
    string rotateString(string A, int offset){
        if(A.empty() || A.size() == 0){
            return A;
        }
        int len = A.size();
        offset %= len;
        reverse(A, 0, len - offset - 1);
        reverse(A, len - offset, len - 1);
        reverse(A, 0, len - 1);
        return A;
    }
private:
    void reverse(string &str, int start, int end){
        while(start < end){
            char temp = str[start];
            str[start] = str[end];
            str[end] = temp;
            start++;
            end--;
        }
    }
};

 

1字符串

标签:false   排序   div   put   sar   art   fga   rest   stat   

原文地址:https://www.cnblogs.com/leosirius/p/8278189.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!