Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,923 questions

51,856 answers

573 users

How to check if a string can split into 4 distinct substrings in Java

2 Answers

0 votes
import java.util.HashSet;
 
public class MyClass {
    private static boolean canSplitInto4DistinctSubstrings(final String str) {
        int size = str.length();
     
        if (size < 4) {
            return false;
        }
     
        for (int i = 1; i < size - 2; i++) {
            for (int j = i + 1; j < size - 1; j++) {
                for (int k = j + 1; k < size; k++) {
                    String part1 = str.substring(0, i);
                    String part2 = str.substring(i, j);
                    String part3 = str.substring(j, k);
                    String part4 = str.substring(k, size);
     
                    if (part1.length() > 0 && part2.length() > 0 && part3.length() > 0 && part4.length() > 0) {
                        HashSet<String> unique_parts = new HashSet<>(); 
                        unique_parts.add(part1);
                        unique_parts.add(part2);
                        unique_parts.add(part3);
                        unique_parts.add(part4);
                        if (unique_parts.size() == 4) {
                            System.out.println(part1 + " " + part2 + " " + part3 + " " + part4);
                            return true;
                        }
                    }
                }
            }
        }
     
        return false;
    }
    public static void main(String args[]) {
        String str = "AlbusDumbledore";
 
        if (canSplitInto4DistinctSubstrings(str)) {
            System.out.print("yes");
        }
        else {
            System.out.print("no");
        }
    }
}
 
 
 
 
 
/*
run:
  
A l b usDumbledore
yes
  
*/

 



answered Jul 26, 2023 by avibootz
edited Feb 14, 2024 by avibootz
0 votes
import java.util.HashSet;
 
public class MyClass {
    private static boolean canSplitInto4DistinctSubstrings(final String str) {
        int size = str.length();
     
        if (size < 4) {
            return false;
        }
     
        for (int i = 2; i < size - 2; i++) {
            for (int j = i + 2; j < size - 1; j++) {
                for (int k = j + 2; k < size; k++) {
                    String part1 = str.substring(0, i);
                    String part2 = str.substring(i, j);
                    String part3 = str.substring(j, k);
                    String part4 = str.substring(k, size);
     
                    if (part1.length() > 0 && part2.length() > 0 && part3.length() > 0 && part4.length() > 0) {
                        HashSet<String> unique_parts = new HashSet<>(); 
                        unique_parts.add(part1);
                        unique_parts.add(part2);
                        unique_parts.add(part3);
                        unique_parts.add(part4);
                        if (unique_parts.size() == 4) {
                            System.out.println(part1 + " " + part2 + " " + part3 + " " + part4);
                            return true;
                        }
                    }
                }
            }
        }
     
        return false;
    }
    public static void main(String args[]) {
        String str = "AlbusDumbledore";
 
        if (canSplitInto4DistinctSubstrings(str)) {
            System.out.print("yes");
        }
        else {
            System.out.print("no");
        }
    }
}
 
 
 
 
 
/*
run:
  
Al bu sD umbledore
yes
  
*/

 



answered Jul 26, 2023 by avibootz
edited Feb 14, 2024 by avibootz
...