Open In App

JavaScript Program to Check if Given String can be Split into Four Distinct Strings

Last Updated : 18 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A string can be split into four distinct strings by identifying specific delimiters or patterns within the original string and extracting substrings based on these divisions, resulting in four separate and non-overlapping text segments. In this article, we will check if a given string can be split into four distinct strings.

Approach 1: Using Regular Expression

In this approach, Regular Expression, or regex, is a pattern-matching technique used in programming to search, match, and manipulate text based on specific patterns or rules, enabling versatile text processing and validation.

It first checks if the string’s length is divisible by 4. If so, it uses regex to split the string into segments and checks if there are four unique segments.

Syntax:

const regex = new RegExp(`.{${inputString.length / 4}}`, 'g');

Example: In this example we are using above mentioned approach.

Javascript




const inputStr1 = "GeeksforGeeks";
const inputStr2 = "abcdefgh";
  
const checkSplittable = (inputString) => {
    return inputString.length % 4 !== 0
        ? false
        : (() => {
            const regex =
                    new RegExp(
                        `.{${inputString.length / 4}}`, 'g');
            const segments = 
                    inputString.match(regex);
            return segments && new Set(segments).size === 4;
        })();
};
  
console.log(`
    "${inputStr1}" is splittable: 
    ${checkSplittable(inputStr1)}`);
console.log(`
    "${inputStr2}" is splittable: 
    ${checkSplittable(inputStr2)}`);


Output

    "GeeksforGeeks" is splittable: 
    false

    "abcdefgh" is splittable: 
    true

Approach 2: Using Recursive Method

Here we are using recursive approach to determine if a given string can be split into four distinct substrings. It checks if a substring can be split further and maintains distinctness, returning “Yes” if possible, “No” otherwise.

Example: In this example we are using above mentioned approach.

Javascript




function isDistinct(s1, s2) {
    return s1.localeCompare(s2) !== 0;
}
function distinctStrings(s) {
    if (s.length < 4) {
        return false;
    }
    function splitAndCheck(s, val) {
        if (val === 0) {
            return true;
        }
        for (let i = 1; i < s.length; i++) {
            const s1 = s.substring(0, i);
            const s2 = s.substring(i);
  
            if (isDistinct(s1, s2)) {
                if (splitAndCheck(s2, val - 1)) {
                    return true;
                }
            }
        }
        return false;
    }
    return splitAndCheck(s, 4);
}
  
const inputStr = "GeeksforGeeks";
if (distinctStrings(inputStr)) {
    console.log("Yes");
} else {
    console.log("No");
}


Output

Yes

Approach 3: Using The Length Property

In this approach, we are using the length property to divide input strings into four equal segments. It checks if the length is divisible by 4 and creates substrings. It ensures each substring’s uniqueness and equal length, determining if the input can be split into four distinct strings.

Example: In this example, we check if a given string can be split into four equal-length, distinct substrings. It verifies divisibility by 4, creates substrings, and ensures uniqueness.

Javascript




let str1 = "GeeksforGeeks";
let str2 = "GeeksforGeeksExample";
  
function isDistinct(str) {
    if (str.length % 4 !== 0) {
        return false;
    }
  
    let length = str.length / 4;
    let substrings = {};
  
    let i = 0;
    for (const char of str) {
        if (i % length === 0) {
            let substring = str.substring(i, i + length);
            if (substrings[substring] || 
                substring.length !== length) {
                return false;
            }
            substrings[substring] = true;
        }
        i++;
    }
  
    return Object.keys(substrings).length === 4;
}
  
console.log(`String: "${str1}"`);
if (isDistinct(str1)) {
    console.log(`result: The string can be 
                split into four distinct strings.`);
} else {
    console.log(`result: The string cannot be 
        split into four distinct strings.`);
}
  
console.log(`\nString: "${str2}"`);
if (isDistinct(str2)) {
    console.log(`result: The string can be 
        split into four distinct strings.`);
} else {
    console.log(`result: The string cannot 
        be split into four distinct strings.`);
};


Output

String: "GeeksforGeeks"
result: The string cannot be 
        split into four distinct strings.

String: "GeeksforGeeksExample"
result: The string can be 
        split into four distinct strings.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads