目录
示例示例
方法 1
算法
示例
输出
方法二
首页 后端开发 C++ 计算通过交换给定数组中字符串对的第一个字符而得到的新字符串对的数量

计算通过交换给定数组中字符串对的第一个字符而得到的新字符串对的数量

Sep 16, 2023 pm 06:49 PM
数组 计算 交换

计算通过交换给定数组中字符串对的第一个字符而得到的新字符串对的数量

在这个问题中,我们需要选择一对字符串并交换它们的第一个字符。之后,我们需要计算新对的总数。我们可以通过交换每对的第一个字符并检查它是否存在于数组中来解决这个问题。

解决这个问题的高效方法是使用哈希映射数据结构。

问题陈述 - 我们有一个包含N个字符串的数组。我们可以从所有数组元素中选择任意两个字符串,并交换这两个字符串的第一个字符。我们需要计算生成的新字符串对的总数,这些新字符串对在数组中不存在。

示例示例

输入 – array[] = {"should", "would", "can"};

输出 – 3

解释 – 新生成的对可以是could和wan。另一对可以是whould和sould。第三对可以是san和chould。

输入 – array[] = {"demo", "test"};

输出 – 1

说明 – 数组中不存在的新生成的对是 temo 和 dest。

方法 1

在这种方法中,我们将使用两个嵌套循环来获取所有数组元素对。之后,我们将交换两对的第一个字符。接下来,我们将使用第三个嵌套循环来检查数组是否包含该对。

算法

  • 定义“cnt”变量并初始化为 0 以存储对的总数。

  • 使用两个嵌套循环遍历字符串数组,并获取每对数组元素。

  • 获取当前对的两个字符串。

  • 如果两个字符串的第一个字符不相等,则交换它们

  • 定义“isFirst”和“isSecond”变量并使用 false 进行初始化,以跟踪新生成的字符串是否存在于数组中。

  • 使用第三个嵌套循环来检查数组中是否有新生成的字符串。另外,根据数组中的字符串更新 isFirst 和 isSecond 变量的值。

  • 如果数组中既没有第一个字符串,也没有第二个字符串,则将‘cnt’的值增加1。

  • 返回‘cnt’变量的值。

示例

#include <iostream>
using namespace std;
// function to find the count of pairs of strings that can be formed by swapping the first character of the strings
int newStringPairs(string array[], int len){
   // Stores the count of pairs
   int cnt = 0;
   // Get all the pairs of strings
   for (int i = 0; i < len; i++){
      for (int j = i + 1; j < len; j++){
         // store single pair of strings
         string first = array[i], second = array[j];
         // If both strings' first characters are not equal, swap them.
         if (first[0] != second[0]){
            swap(first[0], second[0]);
            bool isFirst = false;
            bool isSecond = false;
            // Check whether the strings are present in the array or not
            for (int k = 0; k < len; k++){
               if (array[k] == first){
                  isFirst = true;
               }
                  if (array[k] == second){
                     isSecond = true;
                  }
              }
               // If both the strings are present in the array, then increment the cnt by 1
                if (isFirst == false && isSecond == false){
                    cnt++;
              }
          }
       }
    }
    return cnt;
}
int main(){
   string array[] = {"should", "would", "can"};
   int N = sizeof(array) / sizeof(array[0]);
   cout << "Total number of new pairs we can generate by swapping the first characters of given strings is " << newStringPairs(array, N);
   return 0;
}
登录后复制

输出

Total number of new pairs we can generate by swapping the first characters of given strings is 3
登录后复制
登录后复制

时间复杂度 - O(N^3),因为我们使用了三个嵌套循环。

空间复杂度 – O(1)

方法二

在这种方法中,我们将使用地图数据结构来存储地图中的所有数组值。之后,我们可以检查地图是否包含新生成的字符串。如果没有,我们可以将计数值增加 1。

算法

  • 定义变量‘cnt’

  • 遍历字符串数组,并将所有数组值存储在映射中。

  • 使用两个嵌套循环来获取数组元素的所有配对。

  • 获取字符串对并将它们存储在“first”和“second”变量中。

  • 如果两个字符串的第一个字符不相等,则交换它们。

  • 在地图中,检查是否包含新生成的字符串。如果不是,请将“cnt”的值增加 1。

  • 返回‘cnt’值。

示例

#include <iostream>
#include <unordered_map>
using namespace std;

// function to find the count of pairs of strings that can be formed by swapping the first character of the strings
int newStringPairs(string array[], int len){
    // to store the total number of new pairs
    int cnt = 0;
    // add all strings to the array map
    unordered_map<string, int> str_map;
    for (int i = 0; i < len; i++){
       str_map[array[i]]++;
    }
    //find all pairs of strings that can be formed by swapping the first character of the strings
    for (int i = 0; i < len; i++){
       for (int j = i + 1; j < len; j++){
          // get the current pair of strings
          string first = array[i];
          string second = array[j];
          // If the first character of both strings is not the same, then swap them
          if (first[0] != second[0]){
             swap(first[0], second[0]);
             // If both strings are not present in the map, then the increment count
             if (str_map[first] == 0 && str_map[second] == 0){
                cnt++;
               }
            }
         }
      }
   return cnt;
}
int main(){
   string array[] = {"should", "would", "can"};
   int N = sizeof(array) / sizeof(array[0]);
   cout << "Total number of new pairs we can generate by swapping the first characters of given strings is " << newStringPairs(array, N);
   return 0;
}
登录后复制

输出

Total number of new pairs we can generate by swapping the first characters of given strings is 3
登录后复制
登录后复制

时间复杂度 - O(N^2),因为我们使用了两个嵌套循环。

空间复杂度 – O(N),因为我们使用映射来存储所有数组元素。

我们通过交换任何数组元素的第一个字符来了解新生成的对的总数。我们对第二种方法的代码在时间复杂度上进行了优化,但第一种代码在空间复杂度上更好。

以上是计算通过交换给定数组中字符串对的第一个字符而得到的新字符串对的数量的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

CUDA之通用矩阵乘法:从入门到熟练! CUDA之通用矩阵乘法:从入门到熟练! Mar 25, 2024 pm 12:30 PM

通用矩阵乘法(GeneralMatrixMultiplication,GEMM)是许多应用程序和算法中至关重要的一部分,也是评估计算机硬件性能的重要指标之一。通过深入研究和优化GEMM的实现,可以帮助我们更好地理解高性能计算以及软硬件系统之间的关系。在计算机科学中,对GEMM进行有效的优化可以提高计算速度并节省资源,这对于提高计算机系统的整体性能至关重要。深入了解GEMM的工作原理和优化方法,有助于我们更好地利用现代计算硬件的潜力,并为各种复杂计算任务提供更高效的解决方案。通过对GEMM性能的优

如何使用 foreach 循环去除 PHP 数组中的重复元素? 如何使用 foreach 循环去除 PHP 数组中的重复元素? Apr 27, 2024 am 11:33 AM

使用foreach循环去除PHP数组中重复元素的方法如下:遍历数组,若元素已存在且当前位置不是第一个出现的位置,则删除它。举例而言,若数据库查询结果存在重复记录,可使用此方法去除,得到不含重复记录的结果。

PHP 数组键值翻转:不同方法的性能对比分析 PHP 数组键值翻转:不同方法的性能对比分析 May 03, 2024 pm 09:03 PM

PHP数组键值翻转方法性能对比表明:array_flip()函数在大型数组(超过100万个元素)下比for循环性能更优,耗时更短。手动翻转键值的for循环方法耗时相对较长。

PHP数组深度复制的艺术:使用不同方法实现完美复制 PHP数组深度复制的艺术:使用不同方法实现完美复制 May 01, 2024 pm 12:30 PM

PHP中深度复制数组的方法包括:使用json_decode和json_encode进行JSON编码和解码。使用array_map和clone进行深度复制键和值的副本。使用serialize和unserialize进行序列化和反序列化。

PHP数组多维排序实战:从简单到复杂场景 PHP数组多维排序实战:从简单到复杂场景 Apr 29, 2024 pm 09:12 PM

多维数组排序可分为单列排序和嵌套排序。单列排序可使用array_multisort()函数按列排序;嵌套排序需要递归函数遍历数组并排序。实战案例包括按产品名称排序和按销售量和价格复合排序。

深度复制PHP数组的最佳实践:探索高效的方法 深度复制PHP数组的最佳实践:探索高效的方法 Apr 30, 2024 pm 03:42 PM

在PHP中执行数组深度复制的最佳实践是:使用json_decode(json_encode($arr))将数组转换为JSON字符串,然后再将其转换回数组。使用unserialize(serialize($arr))将数组序列化为字符串,然后将其反序列化为新数组。使用RecursiveIteratorIterator迭代器对多维数组进行递归遍历。

PHP 数组分组函数在数据整理中的应用 PHP 数组分组函数在数据整理中的应用 May 04, 2024 pm 01:03 PM

PHP的array_group_by函数可根据键或闭包函数对数组中的元素分组,返回一个关联数组,其中键是组名,值是属于该组的元素数组。

PHP 数组合并去重算法:并行的解决方案 PHP 数组合并去重算法:并行的解决方案 Apr 18, 2024 pm 02:30 PM

PHP数组合并去重算法提供了并行的解决方案,将原始数组分成小块并行处理,主进程合并块的结果去重。算法步骤:分割原始数组为均等分配的小块。并行处理每个块去重。合并块结果并再次去重。

See all articles