LeetCode //C - 1657. Determine if Two Strings Are Close

1657. Determine if Two Strings Are Close

Two strings are considered close if you can attain one from the other using the following operations:

  • Operation 1: Swap any two existing characters.
    • For example, abcde -> aecdb
  • Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character.
    • For example, aacabb -> bbcbaa (all a’s turn into b’s, and all b’s turn into a’s)
      You can use the operations on either string as many times as necessary.

Given two strings, word1 and word2, return true if word1 and word2 are close, and false otherwise.
 

Example 1:

Input: word1 = “abc”, word2 = “bca”
Output: true
Explanation: You can attain word2 from word1 in 2 operations.
Apply Operation 1: “abc” -> “acb”
Apply Operation 1: “acb” -> “bca”

Example 2:

Input: word1 = “a”, word2 = “aa”
Output: false
Explanation: It is impossible to attain word2 from word1, or vice versa, in any number of operations.

Example 3:

Input: word1 = “cabbba”, word2 = “abbccc”
Output: true
Explanation: You can attain word2 from word1 in 3 operations.
Apply Operation 1: “cabbba” -> “caabbb”
Apply Operation 2: “caabbb” -> “baaccc”
Apply Operation 2: “baaccc” -> “abbccc”

Constraints:
  • 1 < = w o r d 1. l e n g t h , w o r d 2. l e n g t h < = 1 0 5 1 <= word1.length, word2.length <= 10^5 1<=word1.length,word2.length<=105
  • word1 and word2 contain only lowercase English letters.

From: LeetCode
Link: 1657. Determine if Two Strings Are Close


Solution:

Ideas:
  1. Swapping any two existing characters.
  2. Transforming every occurrence of one existing character into another existing character, and vice versa.

Based on the problem statement, we can deduce a few key points to determine if two strings are close:

  • The two strings must have the same length because we can’t add or remove characters.
  • They must have the same set of unique characters.
  • The frequency of each character in one string must be the same as the frequency of some character in the other string. For example, if word1 has three 'a’s, then word2 must have three of some character, but not necessarily 'a’s.
Code:
int compare(const void* a, const void* b) {
    
    
    return (*(int*)a - *(int*)b);
}

bool closeStrings(char* word1, char* word2) {
    
    
    if (strlen(word1) != strlen(word2)) return false;

    int count1[26] = {
    
    0}, count2[26] = {
    
    0};
    bool exist1[26] = {
    
    false}, exist2[26] = {
    
    false};

    // Count the frequency of each character in both strings
    // and mark the existence of characters.
    for (int i = 0; word1[i] != '\0'; i++) {
    
    
        count1[word1[i] - 'a']++;
        exist1[word1[i] - 'a'] = true;

        count2[word2[i] - 'a']++;
        exist2[word2[i] - 'a'] = true;
    }

    // Check if both strings have the same set of characters.
    for (int i = 0; i < 26; i++) {
    
    
        if (exist1[i] != exist2[i]) return false;
    }

    // Sort the frequency arrays to compare frequencies.
    qsort(count1, 26, sizeof(int), compare);
    qsort(count2, 26, sizeof(int), compare);

    // Compare the sorted frequency arrays.
    for (int i = 0; i < 26; i++) {
    
    
        if (count1[i] != count2[i]) return false;
    }

    return true;
}

おすすめ

転載: blog.csdn.net/navicheung/article/details/135332110