Wednesday, 8 January 2014

Java : Check if a string is an interleaved sequence of other 2 strings

public class HelloWorld{

     public static void main(String []args){
       String s1 = "ravi", s2 = "123456789";
StringBuilder sb = new StringBuilder();

int minLength = Math.min(s1.length(), s2.length());
for (int i = 0; i < minLength; i++){
    sb.append(s1.charAt(i));
    sb.append(s2.charAt(i));
}

for (int i = minLength; i < s1.length(); i++){
    sb.append(s1.charAt(i));
}

for (int i = minLength; i < s2.length(); i++){
    sb.append(s2.charAt(i));
}
String s3="vreornaaldo";
if(s3.equals(sb)){
    System.out.println("OKAY");
}
System.out.println(sb.toString());


       }
}

No comments:

Post a Comment