LintCode天梯(USGiants)-String

题目列表:

1 - String

158. Two Strings Are Anagrams Easy

Description:
Write a method anagram(s,t) to decide if two strings are anagrams or not.
中文:
写出一个函数 anagram(s, t) 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串。

class Solution {
public:
    /**
     * @param s: The first string
     * @param b: The second string
     * @return true or false
     */
    bool anagram(string s, string t) {
        // write your code here
        sort(s.begin(),s.end());
        sort(t.begin(),t.end());
        if(s==t) return true;
        else return false;
    }
};
55. Compare Strings Easy

Description:
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.

描述:
比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 大写字母。

class Solution {
public:
    /**
     * @param A: A string includes Upper Case letters
     * @param B: A string includes Upper Case letter
     * @return:  if string A contains all of the characters in B return true 
     *           else return false
     */
    bool compareStrings(string A, string B) {
        // write your code here
        int a[26]={0},b[26]={0};
        memset(a,0,sizeof(a));
        for(int i=0;i<A.size();i++){
            a[A[i]-'A']++;
        }
        int ok=true;
        for(int i=0;i<B.size();i++){
            b[B[i]-'A']++;
        }
        for(int i=0;i<26;i++){
            if(b[i]&&b[i]>a[i]) ok=false;
        }
        return ok;

    }
};
13. strStr Easy

Description:
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.
中文描述:
对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1。

Code:

class Solution {
public:
    /**
     * Returns a index to the first occurrence of target in source,
     * or -1  if target is not part of source.
     * @param source string to be scanned.
     * @param target string containing the sequence of characters to match.
     */
    int strStr(const char *source, const char *target) {
        // write your code here
        if(source==NULL || target==NULL) return -1;
        int k=-1;
        if(strstr(source,target)) k=strstr(source,target)-source;
        return k;
    }
};

171. Anagrams Medium

Description:
Given an array of strings, return all groups of strings that are anagrams.
中文描述:
给出一个字符串数组S,找到其中所有的乱序字符串(Anagram)。如果一个字符串是乱序字符串,那么他存在一个字母集合相同,但顺序不同的字符串也在S中。
Code:

class Solution {
public:    
    /**
     * @param strs: A list of strings
     * @return: A list of strings
     */
    vector<string> anagrams(vector<string> &strs) {
        // write your code here
        vector<string> res,ans;
        int n=strs.size();
        map<string,int> rescnt;
        string s;
        for(int i=0;i<n;i++){
            s=strs[i];
            sort(s.begin(),s.end());
            rescnt[s]++; 
            res.push_back(strs[i]);
        }
        vector<string>::iterator it;
        for(it=res.begin();it!=res.end();++it){
            s= *it;
            sort(s.begin(),s.end());
            if(rescnt[s]>1) ans.push_back(*it);

        }
        return ans;
    }
};
79. Longest Common Substring Medium

Description:
Given two strings, find the longest common substring.
Return the length of it.
中文描述:
给出两个字符串,找到最长公共子串,并返回其长度。
Code:

class Solution:
    # @param A, B: Two string.
    # @return: the length of the longest common substring.
    def longestCommonSubstring(self, A, B):
        # write your code here
        ans = 0
        for i in xrange(len(A)):
            for j in xrange(len(B)):
                k = 0
                while i+k<len(A) and j+k<len(B) and A[i+k]==B[j+k]:
                    k += 1
                if k >ans : ans = k
        return ans
78. Longest Common Prefix

Description:
Given k strings, find the longest common prefix (LCP).
中文描述:
给k个字符串,求出他们的最长公共前缀(LCP)。
Code:

class Solution {
public:    
    /**
     * @param strs: A list of strings
     * @return: The longest common prefix
     */
    string longestCommonPrefix(vector<string> &strs) {
        // write your code here
        int n = strs.size();
        if(n==0) return "";
        if(n==1) return strs[0];
        int k=0,len=1000000;
        for(int i=1;i<n;i++){
            len=len<strs[i].size()?len:strs[i].size();

        }
        int cnt=0;
        while(k<len){
            int ok=1;
            for(int i=1;i<n;i++){
                if(strs[i][k]!=strs[0][k]){
                    ok=0;
                    break;
                }
            }
            if(ok) cnt++;
            ++k;
        }
        string res="";
        for(int i=0;i<cnt;i++){
            res+=strs[0][i];
        }

        return res;
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值