-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion1768.java
More file actions
63 lines (49 loc) · 1.17 KB
/
Copy pathQuestion1768.java
File metadata and controls
63 lines (49 loc) · 1.17 KB
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
62
63
/*
Author: Ananthanarayanan R
Section: Algorithms
Question: 1768
Decription:
*/
public class Question1768
{
public static String mergeAlternately(String word1, String word2)
{
if(word1==null && word2==null)
return null;
if(word1==null && word2 !=null)
return word2;
if(word1!=null && word2==null)
return word1;
if(word1.isEmpty() && word2.isEmpty())
return word1;
if(word1.isEmpty() && !word2.isEmpty())
return word2;
if(!word1.isEmpty() && word2.isEmpty())
return word1;
int diff = Math.abs(word1.length()-word2.length());
int min = Math.min(word1.length(),word2.length());
StringBuilder sb = new StringBuilder();
for(int i =0;i<min;i++)
{
sb.append(word1.charAt(i));
sb.append(word2.charAt(i));
}
if(word1.length()>word2.length())
{
sb.append(word1.substring(min));
}
else
{
sb.append(word2.substring(min));
}
return sb.toString();
}
public static void main(String[] args)
{
System.out.println("Main Method starts");
String s1 = "abcd";
String s2 = "pq";
String s3 = mergeAlternately(s1,s2);
System.out.println(s3);
}
}