Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to check whether two strings contain same characters in same order in Java

1 Answer

0 votes
import java.util.*; 
   
public class MyClass {
    public static String remove_duplicate_characters(String s) {
        String no_dup = "";
        
        for (int i = 0; i < s.length(); i++) {
            if(!no_dup.contains(String.valueOf(s.charAt(i)))) {
                no_dup += String.valueOf(s.charAt(i));
            }
        }
        return no_dup;
    }
       
    public static boolean contain_same_characters_and_order(String s1, String s2) {
        s1 = remove_duplicate_characters(s1);
        s2 = remove_duplicate_characters(s2);

        return s1.equals(s2);
    }
       
    public static void main(String args[]) {
        String s1 = "java programming";
        String s2 = "jjjjjavaaa proooooogrammingggg";

        if (contain_same_characters_and_order(s1, s2))
            System.out.println("yes");
        else
            System.out.println("no");
    }
}
   
   
   
/*
run:
   
yes
   
*/

 



answered Oct 29, 2019 by avibootz
edited Oct 29, 2019 by avibootz

Related questions

...