比较字符串模式的更好解决方案. [英] A better solution for comparing string patterns.?

查看:53
本文介绍了比较字符串模式的更好解决方案.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任务:创建一个函数,如果两个字符串共享相同的字母模式,则该函数返回true,否则返回false.

Task : Create a function that returns true if two strings share the same letter pattern, and false otherwise.

我找到了解决此任务的方法,但我认为它可能更简单,更简短.我将所有相同的字母转换为2个字符串的特定char字符.然后在过程结束时检查它们是否相同.有更简单的解决方案的想法吗?

I found a way to solve this task but I think it could be more simple and short. I converted all same letters to a specific char character for 2 strings. Then end of the process checked whether they are same or not. Any ideas for simpler solutions ?

#include <iostream>
#include <string>

using namespace std;

bool LetterPattern(string str1, string str2) { 
    // Controlling whether they have same size or not
    if (str1.length() != str2.length()) {
        return false; 
    }
    else {
        // Checking for ABC XYZ format type 
        int counter = 0;
        for (int i = 0; i < str1.length()-1; i++) {
            for (int k = i+1; k < str1.length(); k++) {
                if (str1[i] == str1[k]) {
                    counter++;
                }
            }
        }
        int counter2 = 0;
        for (int i = 0; i < str2.length() - 1; i++) {
            for (int k = i + 1; k < str2.length(); k++) {
                if (str2[i] == str2[k]) {
                    counter2++;
                }
            }
        }
        
        if (counter == 0 && counter2 == 0) {
            return true;
        }
        // I added the above part because program below couldn't return 1 for completely different letter formats
        // like XYZ ABC DEF etc.
        
        //Converting same letters to same chars for str1
        for (int i = 0; i < str1.length()-1; i++) {
            for (int k = i+1; k < str1.length(); k++) { 
                if (str1[i] == str1[k]) {
                    str1[k] = (char)i;
                }
            }
            str1[i] = (char)i;
        }
    }
    //Converting same letters to same chars for str1
    for (int i = 0; i < str2.length() - 1; i++) {
        for (int k = i + 1; k < str2.length(); k++) { 
            if (str2[i] == str2[k]) {
                str2[k] = (char)i;
            }
        }
        str2[i] = (char)i;
    }
    if (str1 == str2) { // After converting strings, it checks whether they are same or not
        return true;
    }
    else {
        return false;
    }
}
    

int main(){
    cout << "Please enter two string variable: ";
    string str1, str2;
    cin >> str1 >> str2;
    cout << "Same Letter Pattern: " << LetterPattern(str1, str2);

    system("pause>0");
}

示例:

<身体>
str1 str2 结果
AABB CCDD true
ABAB CDCD true
AAFFG AAFGF false
asdasd qweqwe true

推荐答案

要查看一个字符串是否是另一个字符串的凯撒密码,您可以这样做:

As you want to see if one string is a Caesar cipher of the other, you might do:

bool LetterPatternImpl(const std::string& str1, const std::string& str2) { 
    if (str1.length() != str2.length()) { return false; }

    std::array<std::optional<char>, 256> mapping; // char has limited range,
                                                  // else we might use std::map
    for (std::size_t i = 0; i != str1.length(); ++i) {
        auto index = static_cast<unsigned char>(str1[i]);

        if (!mapping[index]) { mapping[index] = str2[i]; }
        if (*mapping[index] != str2[i]) { return false; }
    }
    return true;
}

bool LetterPattern(const std::string& str1, const std::string& str2) {
    // Both ways needed
    // so ABC <-> ZZZ should return false.
    return LetterPatternImpl(str1, str2) && LetterPatternImpl(str2, str1);
}

这篇关于比较字符串模式的更好解决方案.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆