Given two strings a and b, form a new string of length l, from these strings by combining the prefix of string a and suffix of string b.
Examples :
Input : string a = remuneration
string b = acquiesce
length of pre/suffix(l) = 5
Output :remuniesce
Input : adulation
obstreperous
6
Output :adulatperous
Approach :
- Get first l letters from string a, and last l letters from string b.
- Combine both results, and this will be resultant string.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
string GetPrefixSuffix(string a, string b, int l)
{
string prefix = a.substr(0, l);
int lb = b.length();
string suffix = b.substr(lb - l);
return (prefix + suffix);
}
int main()
{
string a = "remuneration" ,
b = "acquiesce" ;
int l = 5;
cout << GetPrefixSuffix(a, b, l);
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static String prefixSuffix(String a,
String b,
int l)
{
String prefix = a.substring( 0 , l);
int lb = b.length();
String suffix = b.substring(lb - l);
return (prefix + suffix);
}
public static void main(String args[])
throws IOException
{
String a = "remuneration" ,
b = "acquiesce" ;
int l = 5 ;
System.out.println(prefixSuffix(a, b, l));
}
}
|
Python3
def GetPrefixSuffix(a, b, l):
prefix = a[: l];
lb = len (b);
suffix = b[lb - l:];
return (prefix + suffix);
a = "remuneration" ;
b = "acquiesce" ;
l = 5 ;
print (GetPrefixSuffix(a, b, l));
|
C#
using System;
class GFG
{
public static String prefixSuffix(String a,
String b,
int l)
{
String prefix = a.Substring(0, l);
int lb = b.Length;
String suffix = b.Substring(lb - l);
return (prefix + suffix);
}
public static void Main()
{
String a = "remuneration" ,
b = "acquiesce" ;
int l = 5;
Console.Write(prefixSuffix(a, b, l));
}
}
|
PHP
<?php
function GetPrefixSuffix( $a , $b , $l )
{
$prefix = substr ( $a , 0, $l );
$lb = strlen ( $b );
$suffix = substr ( $b , $lb - $l );
return ( $prefix . $suffix );
}
$a = "remuneration" ;
$b = "acquiesce" ;
$l = 5;
echo GetPrefixSuffix( $a , $b , $l );
?>
|
Javascript
<script>
function prefixSuffix(a, b, l)
{
var prefix = a.substring(0, l);
var lb = b.length;
var suffix = b.substring(lb - l);
return (prefix + suffix);
}
var a = "remuneration" ,
b = "acquiesce" ;
var l = 5;
document.write(prefixSuffix(a, b, l));
</script>
|
Time Complexity: O(n + m), where n and m are the lengths of the given string.
Auxiliary Space: O(n + m), where n and m are the lengths of the given string.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!