热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

通过连接字符串数组中的字符来最大化字符串的长度

通过连接字符串数组中的字符来最大化字符串的长度原文:https:

通过连接字符串数组中的字符来最大化字符串的长度

原文:https://www.geeksforgeeks.org/maximize-length-of-the-string-by-concatenating-characters-from-an-array-of-strings/

给定字符串数组arr[],任务是找到可以通过连接给定数组的子序列而生成的不同字符字符串的最大可能长度。

示例

输入arr [] = {"ab", "cd", "ab"}

输出:4

说明:所有可能的组合为{"", "ab", "cd", "abcd", "cdab"}

因此,最大长度可能是 4。

输入arr [] = {"abcdefgh"}

输出:8

说明

所有可能的组合是:"" , "abcdefgh"

因此,最大长度为 8。

方法:想法是使用递归。

请按照以下步骤解决问题:


  • 从左到右迭代,并将每个字符串视为可能的起始子字符串。


  • 初始化HashSet,以存储到目前为止遇到的不同字符。


  • 一旦选择了一个字符串作为起始子字符串,请检查是否还有其余的字符串(如果它仅包含以前没有出现过的字符)。 将此字符串作为子字符串追加到正在生成的当前字符串中。


  • 执行上述步骤后,打印已生成的字符串的最大长度。


下面是上述方法的实现:

C++

// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to check if all the
// string characters are unique
bool check(string s)
{
    set a;
    // Check for repetation in
    // characters
    for (auto i : s) {
        if (a.count(i))
            return false;
        a.insert(i);
    }
    return true;
}
// Funcyion to generate all possible strings
// from the given array
vector helper(vector& arr,
                    int ind)
{
    // Base case
    if (ind == arr.size())
        return { "" };
    // Consider every string as
    // a starting substring and
    // store the generated string
    vector tmp
        = helper(arr, ind + 1);
    vector ret(tmp.begin(),
                    tmp.end());
    // Add current string to result of
    // other strings and check if
    // characters are unique or not
    for (auto i : tmp) {
        string test = i + arr[ind];
        if (check(test))
            ret.push_back(test);
    }
    return ret;
}
// Function to find the maximum
// possible length of a string
int maxLength(vector& arr)
{
    vector tmp = helper(arr, 0);
    int len = 0;
    // Return max length possible
    for (auto i : tmp) {
        len = len > i.size()
                ? len
                : i.size();
    }
    // Return the answer
    return len;
}
// Driver Code
int main()
{
    vector s;
    s.push_back("abcdefgh");
    cout <    return 0;
}

Java

// Java program to implement 
// the above approach
import java.util.*;
import java.lang.*;
class GFG{
// Function to check if all the
// string characters are unique
static boolean check(String s)
{
    HashSet a = new HashSet<>();
    // Check for repetation in
    // characters
    for(int i = 0; i     {
        if (a.contains(s.charAt(i)))
        {
            return false;
        }
        a.add(s.charAt(i));
    }
    return true;
}
// Function to generate all possible
//  strings from the given array
static ArrayList helper(ArrayList arr,
                                int ind)
{
    ArrayList fin = new ArrayList<>();
    fin.add("");
    // Base case
    if (ind == arr.size() )
        return fin;
    // Consider every string as
    // a starting substring and
    // store the generated string
    ArrayList tmp = helper(arr, ind + 1);
    ArrayList ret = new ArrayList<>(tmp);
    // Add current string to result of
    // other strings and check if
    // characters are unique or not
    for(int i = 0; i     {
        String test = tmp.get(i) +
                      arr.get(ind);
        if (check(test))
            ret.add(test);
    }
    return ret;
}
// Function to find the maximum
// possible length of a string
static int maxLength(ArrayList arr)
{
    ArrayList tmp = helper(arr, 0);
    int len = 0;
    // Return max length possible
    for(int i = 0; i     {
        len = len > tmp.get(i).length() ? len : 
                    tmp.get(i).length();
    }
    // Return the answer
    return len;
}
// Driver code
public static void main (String[] args)
{
    ArrayList s = new ArrayList<>();
    s.add("abcdefgh");
    System.out.println(maxLength(s));
}
}
// This code is contributed by offbeat

C

// C# program to implement
// the above approach
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
class GFG{
// Function to check if all the
// string characters are unique
static bool check(string s)
{
    HashSet a = new HashSet();
    // Check for repetation in
    // characters
    for(int i = 0; i     {
        if (a.Contains(s[i]))
        {
            return false;
        }
        a.Add(s[i]);
    }
    return true;
}
// Funcyion to generate all possible
// strings from the given array
static ArrayList helper(ArrayList arr,
                        int ind)
{
    // Base case
    if (ind == arr.Count)
        return new ArrayList(){""};
    // Consider every string as
    // a starting substring and
    // store the generated string
    ArrayList tmp = helper(arr, ind + 1);
    ArrayList ret = new ArrayList(tmp);
    // Add current string to result of
    // other strings and check if
    // characters are unique or not
    for(int i = 0; i     {
        string test = (string)tmp[i] +
                    (string)arr[ind];
        if (check(test))
            ret.Add(test);
    }
    return ret;
}
// Function to find the maximum
// possible length of a string
static int maxLength(ArrayList arr)
{
    ArrayList tmp = helper(arr, 0);
    int len = 0;
    // Return max length possible
    for(int i = 0; i     {
        len = len > ((string)tmp[i]).Length ? len :
                    ((string)tmp[i]).Length;
    }
    // Return the answer
    return len;
}
// Driver Code
public static void Main(string[] args)
{
    ArrayList s = new ArrayList();
    s.Add("abcdefgh");
    Console.Write(maxLength(s));
}
}
// This code is contributed by rutvik_56

输出: 

8

时间复杂度O(2 ^ N)

辅助空间O(N * 2 ^ N)





推荐阅读
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 本文详细介绍了Java中的访问器(getter)和修改器(setter),探讨了它们在保护数据完整性、增强代码可维护性方面的重要作用。通过具体示例,展示了如何正确使用这些方法来控制类属性的访问和更新。 ... [详细]
  • 深入理解Java泛型:JDK 5的新特性
    本文详细介绍了Java泛型的概念及其在JDK 5中的应用,通过具体代码示例解释了泛型的引入、作用和优势。同时,探讨了泛型类、泛型方法和泛型接口的实现,并深入讲解了通配符的使用。 ... [详细]
  • Scala 实现 UTF-8 编码属性文件读取与克隆
    本文介绍如何使用 Scala 以 UTF-8 编码方式读取属性文件,并实现属性文件的克隆功能。通过这种方式,可以确保配置文件在多线程环境下的一致性和高效性。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • Java 中 Writer flush()方法,示例 ... [详细]
  • 火星商店问题:线段树分治与持久化Trie树的应用
    本题涉及编号为1至n的火星商店,每个商店有一个永久商品价值v。操作包括每天在指定商店增加一个新商品,以及查询某段时间内某些商店中所有商品(含永久商品)与给定密码值的最大异或结果。通过线段树分治和持久化Trie树来高效解决此问题。 ... [详细]
  • 本文总结了汇编语言中第五至第八章的关键知识点,涵盖间接寻址、指令格式、安全编程空间、逻辑运算指令及数据重复定义等内容。通过详细解析这些内容,帮助读者更好地理解和应用汇编语言的高级特性。 ... [详细]
  • Java 类成员初始化顺序与数组创建
    本文探讨了Java中类成员的初始化顺序、静态引入、可变参数以及finalize方法的应用。通过具体的代码示例,详细解释了这些概念及其在实际编程中的使用。 ... [详细]
  • 汇编语言等号伪指令解析:探究其陡峭的学习曲线
    汇编语言以其独特的特性和复杂的语法结构,一直被认为是编程领域中学习难度较高的语言之一。本文将探讨汇编语言中的等号伪指令及其对初学者带来的挑战,并结合社区反馈分析其学习曲线。 ... [详细]
author-avatar
鸵鸟家的大pp
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有