Substitution string

Given two strings, checks whether one string can be converted to the other by substituting characters.

For example, the string “abacd” replaces a with k, resulting in “KBKCD”

Str1 =” abbCAA “,str2=” BCCDBB”

C -> D, B -> C,a-> B, str1 can be converted to STR2Copy the code

Str1 =”abbc”,str2=” bCCA”

Output: no Explanation: A -> B, B -> C, C -> A will form a loop, cannot complete the conversion.Copy the code
1. Java antithesis
    static int[] parent = new int[26];

    static int find(int x) {
        if(x ! = parent[x]) {return parent[x] = find(parent[x]);
        }
        return x;
    }


    static void join(int x, int y) {
        int px = find(x);
        int pz = find(y);
        if (px != pz) {
            parent[pz] = px;
        }
    }


    static boolean convertible(String s1, String s2) {
        HashMap<Integer, Integer> mp = new HashMap<Integer, Integer>();
        for (int i=0; i < s1.length(); i++) {int s1Index = s1.charAt(i) - 'a';
            int s2Index = s2.charAt(i) - 'a';


            if(! mp.containsKey(s1.charAt(i) -'a')){
                mp.put(s1.charAt(i) - 'a', s2.charAt(i) - 'a');
            } else {
                if (mp.get(s1.charAt(i) - 'a') != s2.charAt(i) - 'a') {
                    return false; }}}for (Map.Entry<Integer, Integer> it : mp.entrySet()) {
            if (Objects.equals(it.getKey(), it.getValue())) {
                continue;
            } else {
                if (find(it.getKey()) == find(it.getValue())) {
                    return false;
                } else{ join(it.getKey(), it.getValue()); }}}return true;

    }

    static void initialize(a) {
        for (int i = 0; i < 26; i++) { parent[i] = i; }}public static void main(String args[]) {
        String s1, s2;
// s1 = "abbcaa";
// s2 = "bccdbb";
        s1 = "abbc";
        s2 = "bcca";


        initialize();
        if (convertible(s1, s2)) {
            System.out.print("yes");
        } else {
            System.out.print("no"); }}Copy the code
2. Python antithesis
class StrReplace(object) :
    def __init__(self) :
        self.parent = [0] * 26
        self.__init_parent__()

    def __init_parent__(self) :
        for i in range(0.26):
            self.parent[i] = i

    def find(self, x) :
        ifx ! = self.parent[x]: self.parent[x] = self.find(self.parent[x])return self.parent[x]
        return x

    def join(self, x, y) :
        px = self.find(x)
        pz = self.find(y)
        ifpx ! = pz: self.parent[pz] = pxdef convertible(self, s1, s2) :
        mp = dict(a)for i in range(len(s1)):
            s1_index = ord(s1[i]) - ord('a')
            s2_index = ord(s2[i]) - ord('a')

            if s1_index not in mp.keys():
                mp[s1_index] = s2_index
            else:
                ifmp.get(s1_index) ! = s2_index:return False
        for key, val in mp.items():
            if key == val:
                continue
            else:
                if self.find(key) == self.find(val):
                    return False
                else:
                    self.join(key, val)
        return True


if __name__=='__main__':
    # s1 = 'abbcaa'
    # s2 = 'bccdbb'

    s1 = 'abbc'
    s2 = 'bcca'

    g = StrReplace()
    result = g.convertible(s1, s2)
    print('result: {}'.format(result))
Copy the code

Judge the perfect cube number

Given two values A and b, check whether the concatenation of the two values is a perfect cube (a perfect cube is when an integer is a perfect cube of another integer, the number is called a perfect cube, also called a cube).

For example: the concatenation of a=6 and b=4 is 64,64 =444, which is a perfect cube number, so the result is yes

The concatenation of a=100,b=1 is 1001,1001 is not a perfect cube, so the result is noCopy the code
1. The python antithesis
def one(a, b) :
    if not isinstance(a, int) or not isinstance(b, int) :return False

    c = int('{} {}'.format(a, b))
    if round(c ** (1 / 3),10) = =int(round(c ** (1/3), 10)) :return True

    return False
Copy the code

Whether an array consists of two unique sequences

Given an array array, check whether the array consists of two unique sequences, one unique sequence of length N meaning that each integer value from 1 to N appears > only once, regardless of the order.

Input: arr[] = {1, 2, 5, 3, 4, 1, 1}

Explanation: The first five values can form a unique sequence, but the last two ones do notCopy the code

Arr [] = {1, 2, 5, 3, 4, 1, 2}

Explanation: The first five values can form a unique sequence of length 5, but the last two also form a unique sequence of length 2Copy the code
1. The Python antithesis
def unique_sub_arr(arr):
    a = []
    b = []
    for val in arr:
        if val not in a:
            a.append(val)
        else:
            b.append(val)

    return len(arr) == len(set(a)) + len(set(b))
    
if __name__=='__main__':
    a = [1, 2, 5, 3, 4, 1, 1]
    b = [1, 2, 5, 3, 4, 1, 2]
    result = unique_sub_arr(b)
    print(result)
Copy the code