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;
}

相关推荐

  1. 1057:简单计算器

    2024-01-03 12:44:02       42 阅读
  2. leetcode 1652.拆炸弹

    2024-01-03 12:44:02       36 阅读
  3. 1657. 确定两个字符串是否接近

    2024-01-03 12:44:02       27 阅读
  4. AcWing 167.木棒

    2024-01-03 12:44:02       48 阅读
  5. 题目 1567: 超级玛丽

    2024-01-03 12:44:02       38 阅读
  6. LeetCode //C - 1657. Determine if Two Strings Are Close

    2024-01-03 12:44:02       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-03 12:44:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-03 12:44:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-03 12:44:02       87 阅读
  4. Python语言-面向对象

    2024-01-03 12:44:02       96 阅读

热门阅读

  1. QT 高DPI解决方案

    2024-01-03 12:44:02       62 阅读
  2. 分布式(4)

    2024-01-03 12:44:02       55 阅读
  3. 分布式(3)

    2024-01-03 12:44:02       59 阅读
  4. 安卓作业001 - 显示学生信息

    2024-01-03 12:44:02       50 阅读
  5. 单片机相关知识点

    2024-01-03 12:44:02       64 阅读
  6. 提升开发效率,程序员都在使用的免费api

    2024-01-03 12:44:02       67 阅读
  7. 【ASP.NET Core 基础知识】--介绍

    2024-01-03 12:44:02       52 阅读
  8. 静态pod

    2024-01-03 12:44:02       57 阅读
  9. Unity游戏引擎的2D碰撞检测

    2024-01-03 12:44:02       72 阅读
  10. 欢迎来到MySQL优化之旅

    2024-01-03 12:44:02       67 阅读