I've written a function to find whether a given string (stripped of spaces) is a palindrome. Unfortunately, it takes too long to run. Any ideas how I can make the below code run faster? (I'm timing out on LeetCode's Online Judge):
public class Solution {
public boolean checkIfPalindrome(String s) {
if (s.length() == 0 || s.length() == 1) {
return true;
}
//if first letter == last letter
char first = s.charAt(0);
char second = s.charAt(s.length() - 1);
if (first == second) {
String shorterString = s.substring(1, s.length() - 1);
return isPalindrome(shorterString);
} else {
return false;
}
}
public String onlyCharacters(String s) {
String toReturn = "";
for (Character c : s.toCharArray()) {
if (Character.isLetter(c)) {
toReturn += c;
}
}
return toReturn;
}
public boolean isPalindrome(String s) {
s = onlyCharacters(s);
return checkIfPalindrome(s);
}
}
This isn't the most optimal way of finding if a string is palindrome or not.
Just loop through n/2 iterations (where n is length of string) and check if character at position i is equal to character at position n-i
If the length of the string s is n then s will be a palindrome if
s[i]=s[n-1-i] for i in range [0,ceil(n/2)] // 0 based index
Code:
public static boolean checkIfPalindrome(String s) {
for(int i=0;i<s.length()/2;i++) {
if(s.charAt(i)!=s.charAt(s.length()-i-1)) {
return false;
}
}
return true;
}
It's an algorithm method called "divide and conquer". But in this case is just to make it n/2 instead of n.
Here is a suitable algorithm that might just help :
1.For i = 1 to n/2
2.If string[i] = string[n-1] then continue in the loop
3.Else break and return false
4.return true
If n is the length of the input string, your code takes O(n^2) operations. This may surprise you because there are no nested loops in your code, but both the substring method and the += operator for Strings require the creation of a new String, which requires copying its contents.
To see this in action, I have inserted
System.out.println(s);
into the isPalindrome() and checkIfPalindrome() methods, and invoked
isPalindrome("doc, note: i dissent. a fast never prevents a fatness. i diet on cod");
This produces the following output:
docnoteidissentafastneverpreventsafatnessidietoncod
ocnoteidissentafastneverpreventsafatnessidietonco
ocnoteidissentafastneverpreventsafatnessidietonco
cnoteidissentafastneverpreventsafatnessidietonc
cnoteidissentafastneverpreventsafatnessidietonc
noteidissentafastneverpreventsafatnessidieton
noteidissentafastneverpreventsafatnessidieton
oteidissentafastneverpreventsafatnessidieto
oteidissentafastneverpreventsafatnessidieto
teidissentafastneverpreventsafatnessidiet
teidissentafastneverpreventsafatnessidiet
eidissentafastneverpreventsafatnessidie
eidissentafastneverpreventsafatnessidie
idissentafastneverpreventsafatnessidi
idissentafastneverpreventsafatnessidi
dissentafastneverpreventsafatnessid
dissentafastneverpreventsafatnessid
issentafastneverpreventsafatnessi
issentafastneverpreventsafatnessi
ssentafastneverpreventsafatness
ssentafastneverpreventsafatness
sentafastneverpreventsafatnes
sentafastneverpreventsafatnes
entafastneverpreventsafatne
entafastneverpreventsafatne
ntafastneverpreventsafatn
ntafastneverpreventsafatn
tafastneverpreventsafat
tafastneverpreventsafat
afastneverpreventsafa
afastneverpreventsafa
fastneverpreventsaf
fastneverpreventsaf
astneverpreventsa
astneverpreventsa
stneverprevents
stneverprevents
tneverprevent
tneverprevent
neverpreven
neverpreven
everpreve
everpreve
verprev
verprev
erpre
erpre
rpr
rpr
p
p
That's quite a wall of text we are asking the computer to compute! We also see that every String is created twice. That's because you needlessly invoke onlyCharacters() in every iteration.
To avoid creating intermediary String instances, you can use a String Builder:
String onlyCharacters(String s) {
StringBuilder toReturn = new StringBuilder();
for (Character c : s.toCharArray()) {
if (Character.isLetter(c)) {
toReturn.append(c);
}
}
return toReturn.toString();
}
Also, it turns out a StringBuilder has a cool method called reverse(), so we can simplify your program to:
boolean isPalindrome(String s) {
StringBuilder letters = new StringBuilder();
for (Character c : s.toCharArray()) {
if (Character.isLetter(c)) {
letters.append(c);
}
}
StringBuilder reversedLetters = new StringBuilder(letters).reverse();
return onlyLetters.equals(reversedLetters);
}
This code only creates 2 StringBuilder objects rather than n Strings, and is therefore about n/2 times faster than your code.
I found this to be faster than any other answer so far:
public class Solution {
public boolean isPalindrome(String s) {
for (int low = 0, high = s.length() - 1;; low++, high--) {
char cLow = 0, cHigh = 0;
// Find the next acceptable character for the increasing index.
while (low < high && !Character.isLetterOrDigit(cLow = s.charAt(low))) {
low++;
}
// Find the previous acceptable character for the decreasing index.
while (low < high && !Character.isLetterOrDigit(cHigh = s.charAt(high))) {
high--;
}
if (low >= high) {
// All previous character comparisons succeeded and we have a palindrome.
return true;
}
if (Character.toUpperCase(cLow) != Character.toUpperCase(cHigh)) {
// This is not a palindrome.
return false;
}
}
}
}
You have only one object: your original String. Every character is tested until we get acceptable characters (Character.isLetter). Then only those are compared.
No temporary object, no superflous checks. Straight to the goal: it does one thing but does it well.
Note: this answers the actual Leetcode OJ answer by checking alphanumerics instead of only letters and by not caring about the case.
You may use this StringBuilder.reverse() to check Palindrome:
private boolean isPalindrome(String str) {
StringBuilder strBuilder = new StringBuilder(str);
return str.equals(strBuilder.reverse().toString());
}
Related
only one pair of parenthsis are in the string and they are balanced, i cannot use methods which internally use for loops like contain, etc and regular expressions are prohibited.
Here is the code that i came up with but it always shows error.
public static String getParenthesis(String str) {
int first = 1 , last = str.length()-2;
if(str.charAt(0) =='(')
{
first = 0;
}
if (str.charAt(str.length()-1) == ')')
last++;
if(str.charAt(str.length()-1) == ')'&& str.charAt(0)=='(')
return str;
return getParenthesis(str.substring(first, last));
}*/
So, for example, given an input string:
Paren(thesis)String
you want to print:
thesis
Lets view this string as a character array and introduce two indices: first and size.
first size (== str.length())
| |_
str: P a r e n ( t h e s i s ) S t r i n g |_|
You want to increment first until you reach the left brace - (.
You want to decrement size until you reach the right brace - ).
The rest is just proper management of indices to satisfy String's substring().
public static String getParenthesis(String str) {
int first = 0, size = str.length();
if (str.charAt(first) != '(')
return getParenthesis(str.substring(first + 1, size));
if (str.charAt(size - 1) != ')')
return getParenthesis(str.substring(first, size - 1));
return str.substring(first + 1, size - 1);
}
To make recursive functions works properly, you need to use extra parameters, but usually you don't want to handle with that in your public function, so you could fix that using another function. Your public function will not be recursive while your private function will be.
public class HelloWorld {
private static String getParenthesisRec(String str, String res, boolean parenthesisFound) {
if (str.length() == 0) {
// Just in case...
return "";
}
char currentChar = str.charAt(0);
if (parenthesisFound && currentChar == ')') {
// Gotcha!
return res;
} else if (parenthesisFound && currentChar != ')') {
res += currentChar;
}
String substring = str.substring(1, str.length());
if (currentChar == '(') {
return HelloWorld.getParenthesisRec(substring, "", true);
}
return HelloWorld.getParenthesisRec(substring, res, parenthesisFound);
}
public static String getParenthesis(String str) {
return HelloWorld.getParenthesisRec(str, "", false);
}
public static void main(String []args) {
System.out.println(HelloWorld.getParenthesis("Example t(o StackOver)flow"));
}
}
As you can see, I just use the public getParenthesis to setup my recursive and private function getParenthesisRec. Again, you could use one single function with extra parameters, but that would be a mess because you must ensure the first call you pass the correct first values to that parameters. This isn't necessary in languages like Python where you can set default values to your parameters, but in Java you can't (you shouldn't do it though, because again, you can mess it setting incorrect values in the first call).
I have this Java code that find if 2 words are anagrams:
public static boolean isAnagram(String word, String anagram) {
char[] chars = word.toCharArray();
for (char ch : chars) {
int index = anagram.indexOf(ch);
if (index != -1) {
anagram = anagram.substring(0, index) + anagram.substring(index + 1, anagram.length());
} else {
return false;
}
}
return anagram.isEmpty();
}
I want to know if I can do this implementation using Streams instead of for-loop. And how? Thank you!
I would use something like that:
public static boolean isAnagram(String word, String anagram) {
PrimitiveIterator.OfInt sortedWord = word.chars().sorted().iterator();
PrimitiveIterator.OfInt sortedAnagram = anagram.chars().sorted().iterator();
while(sortedWord.hasNext() && sortedAnagram.hasNext()) {
if (!sortedWord.next().equals(sortedAnagram.next())) {
return false;
}
}
return !sortedWord.hasNext() && !sortedAnagram.hasNext();
}
It uses two streams to get sorted iterators. After that you can just loop both of them and see if they are equal. If they are you have found two anagrams.
This has also much better performance, because you are creating a new string on every iteration, which takes down the performance on large strings.
The question: Write a method called swapPairs that accepts a String as a parameter and returns that String with each pair of adjacent letters reversed. If the String has an odd number of letters, the last letter is unchanged. For example, the call swapPairs("example") should return "xemalpe" and the call swapPairs("hello there") should return "ehll ohtree".
public static String swapPairs(String s) {
String t="";
if(s.length()%2==0) {
for (int i=0;i<s.length()/2;i++) {
t+=s.charAt(2*i+1)+s.charAt(2*i);
}
}
else {
for (int i=0;i<s.length()/2;i++) {
t+=s.charAt(2*i+1)+s.charAt(2*i);
}
t+=s.charAt(s.length()-1);
}
return t;
}
You can use only 1 for cycles like:
public static String swapPairs(String s) {
StringBuilder t = new StringBuilder();
for(int i = 0; i < s.length() - 1; i += 2) {
t.append(s.charAt(i + 1));
t.append(s.charAt(i));
}
if(s.length() % 2 == 1) { //appending the last character if needed
t.append(s.charAt(s.length() - 1));
}
return t.toString();
}
Also using StringBuilder is a nice idea :)
String#charAt returns a char. Adding two chars will result in another char (because char is a numeric type), not in a String as you seem to expect. The compiler should give you an error message in that direction.
You could use String#substring instead.
You could just amend your method to use a StringBuilder instead and change it something like:
sb.append(s.charAt((2 * i) + 1)).append(s.charAt(2 * i));
Given a pattern , we need to generate all possible binary numbers by filling the missing places in the pattern by 0 and 1.
E.g. Pattern = "x1x";
Output = [010, 110, 011, 111]
I solved it by creating a method calculate.
public static List<String> calculate(String input, int currentIndex) {
List<String> result = new ArrayList<String>();
if(currentIndex > input.length()-1) {
result.add("");
return result;
}
for(String fragment: calculate(input, currentIndex + 1)) {
if(input.charAt(currentIndex)=='x') {
result.add('0' + fragment);
result.add('1' + fragment);
}
else {
result.add(input.charAt(currentIndex) + fragment);
}
}
return result;
}
Is there some way in which we can leverage the given pattern and design a much Faster and/or cleaner solution. I already know that non-recursive solution will be better. Java 8 features are also welcome.
On reflection, using recursion and a call back is much more efficient way of doing this. Note: this creates very few objects (possibly 3 regardless of the number of results).
public static void main(String[] args) {
printForPattern("x1x", System.out::println);
}
private static void printForPattern(String pattern, Consumer<CharSequence> consumer) {
printForPattern(pattern, new StringBuilder(), consumer);
}
private static void printForPattern(String pattern, StringBuilder sb, Consumer<CharSequence> consumer) {
int length = sb.length();
if (pattern.length() == length) {
consumer.accept(sb);
return;
}
char ch = pattern.charAt(length);
if (ch == 'x' || ch == '0') {
sb.append('0');
printForPattern(pattern, sb, consumer);
sb.setLength(length);
}
if (ch == 'x' || ch == '1') {
sb.append('1');
printForPattern(pattern, sb, consumer);
sb.setLength(length);
}
}
To add this to a list you can do
List<String> results = ...
printForPattern("x1x", s -> results.add(x.toString()));
You can;
count the number of wildcards or xs. This is the number of bits you need to iterate over.
iterate over 2^^{number of x's) and this will give you all possible bits for those x.
merge these generated x with the provided bit pattern.
If there are n occurrences of the character x, you can enumerate the possible bit combinations for the x positions by incrementing a counter from 0 to 2^n - 1. Then take one of the bits from the counter to decide for each x if it should be substituted by 0 or 1.
So the outline of the algorithm is:
Count number of occurrences of x.
Loop from 0 to 2^n - 1.
Substitute each x with a bit from the counter.
Output result.
This is limited to 63 occurrences of x, since we run out of room in a long otherwise. But it would take a very, very long time to enumerate more than 2^63 solutions anyway, so I don't think this is a practical concern.
Code:
private static void enumBitPatterns(String pattern) {
int len = pattern.length();
int xCount = 0;
for (int iChar = 0; iChar < len; ++iChar) {
if (pattern.charAt(iChar) == 'x') {
++xCount;
}
}
StringBuilder builder = new StringBuilder(len);
long enumCount = 1L << xCount;
for (long iEnum = 0; iEnum < enumCount; ++iEnum) {
builder.delete(0, len);
long val = iEnum;
for (int iChar = 0; iChar < len; ++iChar) {
char ch = pattern.charAt(iChar);
if (ch == 'x') {
builder.append((char)('0' + (val & 1)));
val >>= 1;
} else {
builder.append(ch);
}
}
System.out.println(builder);
}
}
While recursion is undoubtedly more elegant, it is also easy to write a function which takes a pattern and a binary string, and produces the next binary string according to the pattern. Then you just need to start with the string created by changing all the x's in the pattern to 0s, and iterate through successors until you reach a string which doesn't have one.
To find the successor for a string given a pattern, iterate backwards through both the string and the pattern. At each character position:
If the pattern character is x:
if the string character is 1, change it to 0.
if the string character is 0, change it to 1 and return True.
Otherwise, the pattern character should match the string character. In any event, continue with the loop.
If the loop does not terminate with a return, then there is no successor and the function should return False. At this point, the string is reinitialized to the initial value.
Iterating backwards through the pattern/string produces the values in lexicographic order. If you don't care about the order in which the values are produced, the code might be slightly simpler if you iterate forwards.
In Java, strings are immutable so you can't just mutate the input string. If you need to create a copy of the string, you can't just return where the above algorithm indicates a return; you need to complete the copy. If you use a StringBuilder, it will definitely be easier to work in the opposite direction.
public class Main {
static final class BinaryStringList extends AbstractList<String> {
private final char[] chars;
private final int size;
private final StringBuilder sb = new StringBuilder();
BinaryStringList(String pattern) {
chars = pattern.toCharArray();
int count = 0;
for (char c : chars) {
if (c != '0' && c != '1' && c != 'x') {
throw new IllegalArgumentException();
}
if (c == 'x') {
count++;
if (count > 30) {
throw new IllegalArgumentException();
}
}
}
size = (int) Math.pow(2, count);
}
#Override
public int size() {
return size;
}
#Override
public String get(int i) {
if (i < 0 || i >= size) { throw new IndexOutOfBoundsException(); }
sb.setLength(0);
int place = 0;
for (char a : chars) {
sb.append(a == 'x' ? ((i >> place++ & 1) == 0 ? '0' : '1') : a);
}
return sb.toString();
}
}
public static void main(String[] args) {
System.out.println(new BinaryStringList("0xx1x"));
}
}
The advantage of this approach is that instantiating a new BinaryStringList is virtually instantaneous. It's only when you iterate over it that it actually does any work.
I was asked this question in a phone interview for summer internship, and tried to come up with a n*m complexity solution (although it wasn't accurate too) in Java.
I have a function that takes 2 strings, suppose "common" and "cmn". It should return True based on the fact that 'c', 'm', 'n' are occurring in the same order in "common". But if the arguments were "common" and "omn", it would return False because even though they are occurring in the same order, but 'm' is also appearing after 'o' (which fails the pattern match condition)
I have worked over it using Hashmaps, and Ascii arrays, but didn't get a convincing solution yet! From what I have read till now, can it be related to Boyer-Moore, or Levenshtein Distance algorithms?
Hoping for respite at stackoverflow! :)
Edit: Some of the answers talk about reducing the word length, or creating a hashset. But per my understanding, this question cannot be done with hashsets because occurrence/repetition of each character in first string has its own significance. PASS conditions- "con", "cmn", "cm", "cn", "mn", "on", "co". FAIL conditions that may seem otherwise- "com", "omn", "mon", "om". These are FALSE/FAIL because "o" is occurring before as well as after "m". Another example- "google", "ole" would PASS, but "google", "gol" would fail because "o" is also appearing before "g"!
I think it's quite simple. Run through the pattern and fore every character get the index of it's last occurence in the string. The index must always increase, otherwise return false.
So in pseudocode:
index = -1
foreach c in pattern
checkindex = string.lastIndexOf(c)
if checkindex == -1 //not found
return false
if checkindex < index
return false
if string.firstIndexOf(c) < index //characters in the wrong order
return false
index = checkindex
return true
Edit: you could further improve the code by passing index as the starting index to the lastIndexOf method. Then you would't have to compare checkindex with index and the algorithm would be faster.
Updated: Fixed a bug in the algorithm. Additional condition added to consider the order of the letters in the pattern.
An excellent question and couple of hours of research and I think I have found the solution. First of all let me try explaining the question in a different approach.
Requirement:
Lets consider the same example 'common' (mainString) and 'cmn'(subString). First we need to be clear that any characters can repeat within the mainString and also the subString and since its pattern that we are concentrating on, the index of the character play a great role to. So we need to know:
Index of the character (least and highest)
Lets keep this on hold and go ahead and check the patterns a bit more. For the word common, we need to find whether the particular pattern cmn is present or not. The different patters possible with common are :- (Precedence apply )
c -> o
c -> m
c -> n
o -> m
o -> o
o -> n
m -> m
m -> o
m -> n
o -> n
At any moment of time this precedence and comparison must be valid. Since the precedence plays a huge role, we need to have the index of each unique character Instead of storing the different patterns.
Solution
First part of the solution is to create a Hash Table with the following criteria :-
Create a Hash Table with the key as each character of the mainString
Each entry for a unique key in the Hash Table will store two indices i.e lowerIndex and higherIndex
Loop through the mainString and for every new character, update a new entry of lowerIndex into the Hash with the current index of the character in mainString.
If Collision occurs, update the current index with higherIndex entry, do this until the end of String
Second and main part of pattern matching :-
Set Flag as False
Loop through the subString and for
every character as the key, retreive
the details from the Hash.
Do the same for the very next character.
Just before loop increment, verify two conditions
If highestIndex(current character) > highestIndex(next character) Then
Pattern Fails, Flag <- False, Terminate Loop
// This condition is applicable for almost all the cases for pattern matching
Else If lowestIndex(current character) > lowestIndex(next character) Then
Pattern Fails, Flag <- False, Terminate Loop
// This case is explicitly for cases in which patterns like 'mon' appear
Display the Flag
N.B : Since I am not so versatile in Java, I did not submit the code. But some one can try implementing my idea
I had myself done this question in an inefficient manner, but it does give accurate result! I would appreciate if anyone can make out an an efficient code/algorithm from this!
Create a function "Check" which takes 2 strings as arguments. Check each character of string 2 in string 1. The order of appearance of each character of s2 should be verified as true in S1.
Take character 0 from string p and traverse through the string s to find its index of first occurrence.
Traverse through the filled ascii array to find any value more than the index of first occurrence.
Traverse further to find the last occurrence, and update the ascii array
Take character 1 from string p and traverse through the string s to find the index of first occurence in string s
Traverse through the filled ascii array to find any value more than the index of first occurrence. if found, return False.
Traverse further to find the last occurrence, and update the ascii array
As can be observed, this is a bruteforce method...I guess O(N^3)
public class Interview
{
public static void main(String[] args)
{
if (check("google", "oge"))
System.out.println("yes");
else System.out.println("sorry!");
}
public static boolean check (String s, String p)
{
int[] asciiArr = new int[256];
for(int pIndex=0; pIndex<p.length(); pIndex++) //Loop1 inside p
{
for(int sIndex=0; sIndex<s.length(); sIndex++) //Loop2 inside s
{
if(p.charAt(pIndex) == s.charAt(sIndex))
{
asciiArr[s.charAt(sIndex)] = sIndex; //adding char from s to its Ascii value
for(int ascIndex=0; ascIndex<256; ) //Loop 3 for Ascii Array
{
if(asciiArr[ascIndex]>sIndex) //condition to check repetition
return false;
else ascIndex++;
}
}
}
}
return true;
}
}
Isn't it doable in O(n log n)?
Step 1, reduce the string by eliminating all characters that appear to the right. Strictly speaking you only need to eliminate characters if they appear in the string you're checking.
/** Reduces the maximal subsequence of characters in container that contains no
* character from container that appears to the left of the same character in
* container. E.g. "common" -> "cmon", and "whirlygig" -> "whrlyig".
*/
static String reduceContainer(String container) {
SparseVector charsToRight = new SparseVector(); // Like a Bitfield but sparse.
StringBuilder reduced = new StringBuilder();
for (int i = container.length(); --i >= 0;) {
char ch = container.charAt(i);
if (charsToRight.add(ch)) {
reduced.append(ch);
}
}
return reduced.reverse().toString();
}
Step 2, check containment.
static boolean containsInOrder(String container, String containee) {
int containerIdx = 0, containeeIdx = 0;
int containerLen = container.length(), containeeLen == containee.length();
while (containerIdx < containerLen && containeeIdx < containeeLen) {
// Could loop over codepoints instead of code-units, but you get the point...
if (container.charAt(containerIdx) == containee.charAt(containeeIdx)) {
++containeeIdx;
}
++containerIdx;
}
return containeeIdx == containeeLen;
}
And to answer your second question, no, Levenshtein distance won't help you since it has the property that if you swap the arguments the output is the same, but the algo you want does not.
public class StringPattern {
public static void main(String[] args) {
String inputContainer = "common";
String inputContainees[] = { "cmn", "omn" };
for (String containee : inputContainees)
System.out.println(inputContainer + " " + containee + " "
+ containsCommonCharsInOrder(inputContainer, containee));
}
static boolean containsCommonCharsInOrder(String container, String containee) {
Set<Character> containerSet = new LinkedHashSet<Character>() {
// To rearrange the order
#Override
public boolean add(Character arg0) {
if (this.contains(arg0))
this.remove(arg0);
return super.add(arg0);
}
};
addAllPrimitiveCharsToSet(containerSet, container.toCharArray());
Set<Character> containeeSet = new LinkedHashSet<Character>();
addAllPrimitiveCharsToSet(containeeSet, containee.toCharArray());
// retains the common chars in order
containerSet.retainAll(containeeSet);
return containerSet.toString().equals(containeeSet.toString());
}
static void addAllPrimitiveCharsToSet(Set<Character> set, char[] arr) {
for (char ch : arr)
set.add(ch);
}
}
Output:
common cmn true
common omn false
I would consider this as one of the worst pieces of code I have ever written or one of the worst code examples in stackoverflow...but guess what...all your conditions are met!
No algorithm could really fit the need, so I just used bruteforce...test it out...
And I could just care less for space and time complexity...my aim was first to try and solve it...and maybe improve it later!
public class SubString {
public static void main(String[] args) {
SubString ss = new SubString();
String[] trueconditions = {"con", "cmn", "cm", "cn", "mn", "on", "co" };
String[] falseconditions = {"com", "omn", "mon", "om"};
System.out.println("True Conditions : ");
for (String str : trueconditions) {
System.out.println("SubString? : " + str + " : " + ss.test("common", str));
}
System.out.println("False Conditions : ");
for (String str : falseconditions) {
System.out.println("SubString? : " + str + " : " + ss.test("common", str));
}
System.out.println("SubString? : ole : " + ss.test("google", "ole"));
System.out.println("SubString? : gol : " + ss.test("google", "gol"));
}
public boolean test(String original, String match) {
char[] original_array = original.toCharArray();
char[] match_array = match.toCharArray();
int[] value = new int[match_array.length];
int index = 0;
for (int i = 0; i < match_array.length; i++) {
for (int j = index; j < original_array.length; j++) {
if (original_array[j] != original_array[j == 0 ? j : j-1] && contains(match.substring(0, i), original_array[j])) {
value[i] = 2;
} else {
if (match_array[i] == original_array[j]) {
if (value[i] == 0) {
if (contains(original.substring(0, j == 0 ? j : j-1), match_array[i])) {
value[i] = 2;
} else {
value[i] = 1;
}
}
index = j + 1;
}
}
}
}
for (int b : value) {
if (b != 1) {
return false;
}
}
return true;
}
public boolean contains(String subStr, char ch) {
for (char c : subStr.toCharArray()) {
if (ch == c) {
return true;
}
}
return false;
}
}
-IvarD
I think this one is not a test of your computer science fundamentals, more what you would practically do within the Java programming environment.
You could construct a regular expression out of the second argument, i.e ...
omn -> o.*m[^o]*n
... and then test candidate string against this by either using String.matches(...) or using the Pattern class.
In generic form, the construction of the RegExp should be along the following lines.
exp -> in[0].* + for each x : 2 -> in.lenght { (in[x-1] +
[^in[x-2]]* + in[x]) }
for example:
demmn -> d.*e[^d]*m[^e]*m[^m]*n
I tried it myself in a different way. Just sharing my solution.
public class PatternMatch {
public static boolean matchPattern(String str, String pat) {
int slen = str.length();
int plen = pat.length();
int prevInd = -1, curInd;
int count = 0;
for (int i = 0; i < slen; i++) {
curInd = pat.indexOf(str.charAt(i));
if (curInd != -1) {
if(prevInd == curInd)
continue;
else if(curInd == (prevInd+1))
count++;
else if(curInd == 0)
count = 1;
else count = 0;
prevInd = curInd;
}
if(count == plen)
return true;
}
return false;
}
public static void main(String[] args) {
boolean r = matchPattern("common", "on");
System.out.println(r);
}
}