Skip to content

Instantly share code, notes, and snippets.

@Devendra0110
Created March 11, 2024 12:31
Show Gist options
  • Save Devendra0110/4c8894026d984e77f6ca6c5ca72cf48d to your computer and use it in GitHub Desktop.
Save Devendra0110/4c8894026d984e77f6ca6c5ca72cf48d to your computer and use it in GitHub Desktop.
Given three integers a, b, and c, return the longest possible happy string. If there are multiple longest happy strings, return any of them. If there is no such string, return the empty string ""
a = 1, b=3 ,c =5

let initialString = ''
let seperators = []
match = ''
replacer = ''
if(a>=b){
    if(a>=c){
        initialString = 'a'.repeat(a)
        seperators = 'b'.repeat(b)+'c'.repeat(c)
        match = /a{2,}$/
        replacer = 'aa'
        
    }else{
        initialString = 'c'.repeat(c)
        seperators = 'a'.repeat(a) + 'b'.repeat(b)
        match = /c{2,}$/
        replacer = 'cc'

    }
}else{
    if(b>=c){
        initialString = 'b'.repeat(b)
        seperators = 'a'.repeat(a) + 'c'.repeat(c)
        match = /b{2,}$/
        replacer = 'bb'
    }else{
        initialString = 'c'.repeat(c)
        seperators = 'a'.repeat(a) + 'b'.repeat(c)
        match = /c{2,}$/
        replacer = 'cc'
    }
}
seperators = [...seperators.split("")]
let index=2
console.log(seperators)
while(seperators.length){
    initialString = initialString.slice(0, index) + seperators.shift() + initialString.slice(index);
    console.log(initialString,index)
    index +=3
    if(index > initialString.length){
        index = 3
    }
    

}
initialString = initialString.replace(match, replacer)
console.log(initialString)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment