-
Notifications
You must be signed in to change notification settings - Fork 3
/
AlternateString.java
61 lines (52 loc) · 1.59 KB
/
AlternateString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
You are merging data from two sources connected to a network access
point to create a new data packet.
You must merge strings a and b, and the return a single merged string.
A merge operation on two strings is described as follows.
Append alternating characters from a and b, respectively, to some new
string mergedString.
Once all of the characters in one of the strings have been merged,
append the remaining characters in the other string to mergedString.
As an example, assume you have two strings to merge : 'abc' and 'stuvwx'.
Alternate between the first and second strings as long as you can :
'a' + 's'+'b'+'t'+'c' +'u'. At this point you have traversed the first string
and have generated 'asbtcu'.
The remainder of the second string ,'vwx' is now added to the end of
the string, creating 'asbtcuvwx'.
Sample Testcase:
----------------------------------
input=kmit
ngit
output=
knmgiitt
Sample Testcase:
----------------------------------
input=aceg ikmo
bdfh jlnp
output="abcdefgh ijklmnop"
import java.util.*;
public class Sfff {
public static void main(String ar[]) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String s1 = sc.nextLine();
int a = s.length();
int b = s1.length();
String res = "";
int i =0;
int j= 0;
while(i<a&&j<b)
{
res+=s.charAt(i++);
res+=s1.charAt(j++);
}
while(i<a)
{
res+=s.charAt(i++);
}
while(j<b)
{
res+=s1.charAt(j++);
}
System.out.println(res);
}
}