Set up two indexes, one into each string, and zero them both. In the loop, compare the two characters at the relevant indexes. If the character in string 1 is less than the character in string 2, advance the index to string 1. If this takes it out of string one, you need all the characters of string 2, and you are done. I am coding for the problem in which we got to count the number of common characters in two strings. Main part of the count goes like this. This goes with an O(n^2) logic. However I could not think of a better solution than this. Can anyone help me in coding with an O(n) logic. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings. You can do it manually as: and the regex works fine. This is a common problem when using regex, so the java.util.regex.Pattern class provides a static method named quote which can be used to do exactly this: quote the regex-metacharacters so that they are treated literally. So before using t in replaceAll you quote it as: Program using quote method works as expected. In this chapter, I'll cover the built-in scalar data types: numbers, characters, and strings. Technically, strings aren't true scalars--a string is a sequence of characters, and you can access individual characters and manipulate strings with a function that operates on sequences. 5 Answers 5. active oldest votes. up vote 5 down vote accepted. This is very simple. Take two int arrays freq1 and freq2. Initialize all its elements to 0. Then read your strings and store the frequencies of the characters to these arrays. After that compare the arrays freq1 and freq2 to find the common characters.
http://www.codeproject.com/Questions/192754/C-program-find-common-chars-in-two-strings http://stackoverflow.com/questions/21657544/finding-common-characters-in-two-strings http://docs.oracle.com/javase/tutorial/java/data/strings.html http://stackoverflow.com/questions/4203911/how-do-i-find-the-characters-common-to-two-strings-in-java-using-single-replacea http://gigamonkeys.com/book/numbers-characters-and-strings.html http://stackoverflow.com/questions/21657544/finding-common-characters-in-two-strings
how to find if a character is common in two strings