Problem Statement
You are given a string S consisting of lowercase English characters. The length of S is between 1 and 3,
inclusive. Print the string of length 6 that is a repetition of S.
It can be shown that there uniquely exists such a string under the Constraints of this problem.
It can be shown that there uniquely exists such a string under the Constraints of this problem.
Input
The input consists of a string S.
Constraints
S is a string consisting of lowercase English characters of length between 1 and 3, inclusive.
Constraints
S is a string consisting of lowercase English characters of length between 1 and 3, inclusive.
Output
Print the answer string, which is of length 6.
Solution:-
I have explained detailed solution for this problem on my youtube video. You can go and watch the full video
to understand the problem and solution of this problem in detail. In this video you can understand the logic to
solve this problem. At the end of this blog i will give the link of the video.
I tried to provide the solution of this problem in different languages as well. You can check this as well.
Sample Example: -
Input : abc
Output: abcabc
Explanation: -
So here the output are strings that are repetitions of String given in input S = abc.
So the repetations of this string are abc, abcabc, abcabcabc, abcabcabcabc, and so on.
Out of them abcabc has the length of 6, so abcabc should be the output.
Java Code:-
1st Approach: using if else ladder
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc=new Scanner(System.in);
String str = sc.next();
if(str.length() == 1){
System.out.print( str + str + str + str + str + str);
}
else if(str.length() == 2){
System.out.print( str + str + str);
}
else if(str.length() == 3){
System.out.print( str + str);
}
}
}
2nd Approach:- Using repeat() Method
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc=new Scanner(System.in);
String str = sc.next();
System.out.print(str.repeat(6/str.length()));
}
}
3rd Approach:-
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static void main (String[] args) {
// Your code here
Scanner sc=new Scanner(System.in);
String str = sc.next();
int ans = 6/str.length();
for(int i=0; i<ans; i++){
System.out.print(str);
}
}
}
C++ Code Solution:-
#include <bits/stdc++.h> // header file includes every Standard library
using namespace std;
int main() {
// Your code here
string S;
cin >> S;
int l=S.size();
for(int i=0; i<6/l; i++)
cout << S;
cout << endl;
}
C:- Below is the C implementation of above problem.
#include <stdio.h> // header file for Standard Input Output
#include <stdlib.h> // header file for Standard Library
int main() {
//Your code here
char x[105];
scanf("%s",x);
int a=strlen(x);
int y=6/a;
for(int i=0;i<y;i++){
printf("%s",x);
}
}
JavaScript:- Below is the javascript code.
// Your code here
function Main(input) {
input = input.split('\n')
const inputUnit = input[0].split('')
result = []
while (result.length < 6) {
result.push(...inputUnit)
}
console.log(result.join(''))
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));
Python Code:-
# Your code here
S = input()
print(S*(6//len(S)))
If you liked this article, share it with your friends and colleagues!
No comments: