首页 > 试题广场 >

字母异位词的长度

[编程题]字母异位词的长度
  • 热度指数:1186 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
现有两个仅由小写英文字母构成的字符串s, x,请判断它们是否为字母异位词,如果是的话,输出字母异位词的长度,不是的话,返回-1
注:如果每个字符出现的次数都相同,则称他们为字母异位词

数据范围:

示例1

输入

"aba","aa"

输出

-1

说明

第一个字符串与第二个字符串a出现的次数相同,而b出现的次数不同,不符合每个字符出现的次数都相同。故输出-1 
示例2

输入

"a","a"

输出

1

说明

第一个字符串与第二个字符串每个字符出现的次数都相同,故输出相同的长度为1 
#include <iterator>
#include <string>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param s string字符串
     * @param c string字符串
     * @return int整型
     */
    int isCongruent(string s, string c) {
        // write code here
        long num=0;
        long num1=0;
        set<char>s1(s.begin(),s.end());
        set<char>s2(c.begin(),c.end());
        if(s1!=s2) return -1;      //元素不同直接退出
        if(s.size()!=c.size()) return -1;
        for(int i=0;i<s.size();i++){   记录两个字符串各自地和
            num+=(s[i]-'a');
            num1+=(c[i]-'a');
        }
        if(num==num1)  return s.size();
        else return -1;
    }
};
编辑于 2024-03-12 15:07:31 回复(0)
package main
//import "fmt"

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param s string字符串 
 * @param c string字符串 
 * @return int整型
*/
func isCongruent( s string ,  c string ) int {
    if len(s)!=len(c){
        return -1
    }
    cnt:=map[byte]int{}
    for _,ch:=range []byte(s){
        cnt[ch]++
    }
    for _,ch:=range []byte(c){
        if _,ok:=cnt[ch];!ok{
            return -1
        }else{
            if cnt[ch]==1{
                delete(cnt,ch)
            }else{
                cnt[ch]--
            }
        }
    }
    if len(cnt)==0{
        return len(s)
    }
    return -1
}

发表于 2023-03-09 14:51:58 回复(0)
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param s string字符串 
 * @param c string字符串 
 * @return int整型
 */
int isCongruent(char* s, char* c ) {
    // write code here
    if(strlen(s)!=strlen(c))
       return -1;
    int *hash1=(int *)calloc(26,sizeof(int));
    int *hash2=(int *)calloc(26,sizeof(int));
    for(int i=0;i<strlen(s);i++){
        hash1[s[i]-'a']+=1;
        hash2[c[i]-'a']+=1;
    }
    int count=0;
    for(int j=0;j<26;j++){
       
        if(hash1[j]!=hash2[j])
            return -1;
        if(hash1[j]!=0)
            count+=1;
            
    }
    return strlen(s);


}

发表于 2023-01-12 22:01:41 回复(0)
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param s string字符串 
     * @param c string字符串 
     * @return int整型
     */
    int isCongruent(string sstring c) {
        // write code here
        unordered_map<charint> m;
        unordered_map<charint> m1;
        for(int i=0; i<s.length(); i++){
            m[s[i]]++;
        }
        for(int i=0; i<c.length(); i++){
            m1[c[i]]++;
        }
        for(auto it = m.begin(); it != m.end(); it++){
            if(m1.find(it->first) == m1.end()){
                return -1;
            }
            if(m1[it->first] != it->second){
                return -1;
            }
        }
        return s.length();
    }
};
发表于 2022-11-15 23:13:20 回复(0)
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param s string字符串 
     * @param c string字符串 
     * @return int整型
     */
    int isCongruent(string s, string c) 
    {
        // write code here
        if(s.length()!=c.length())
            return -1;
        vector<int> arr(26,0);
        for(auto &ch:s)
        {
            arr[ch-'a']++;
        }
        for(auto &ch:c)
        {
            arr[ch-'a']--;
            if(arr[ch-'a']<0)
                return -1;
        }
        return s.length();
    }
};

发表于 2022-09-19 20:43:41 回复(0)
class Solution:
    def isCongruent(self , s: str, c: str) -> int:
        # write code here
        return len(s) if sorted(s) == sorted(c) else -1

发表于 2022-07-09 00:21:16 回复(0)
from collections import Counter
class Solution:
    def isCongruent(self , s: str, c: str) -> int:
        # write code here
        d1 = Counter(s)
        d2 = Counter(c)
        if d1==d2:
            return len(s)
        return -1

发表于 2022-04-22 15:17:28 回复(0)