0

The question is to count how many permutations of a string B have an equivalent pattern into a bigger string A. For example, if A="aabbccd" and B="xx", then it should print 3, since "aa", "bb", "cc" are all substrings of A which share the same pattern as B.

I have tried to pass the substrings as numbers, such as xx becomes "11" and do the same for string A, but I still can't get it to work. Any ideas? Length can be up to 10^7.

Here's the code for changing pattern:

void transform(int* dest, char* original, int len) {
    int j=1;
    Al[original[0]-'a']=j;
    dest[0]=j;
    j++;
    for (int i=1;i<len;i++) {
        if (Al[original[i]-'a']==0) 
            Al[original[i]-'a']=j++;
        dest[i]=Al[original[i]-'a'];
    }
}
4
  • 2
    How many characters can B have? What would you consider the permutations of B="xyz";? Commented Apr 12, 2018 at 7:05
  • 2
    You have to define the format of your pattern : the rules that will apply. Because here, we have absolutly no idea how we should interpret "xx". Is the the number of the same character ? How do we interpret "xex" then ? If you have no idea of what you can do, you can take example on the regex, and have a really ligth regex implementation
    – Tom's
    Commented Apr 12, 2018 at 7:14
  • Okay sorry... Both of the strings can take any character of the alphabet as part of the string... xx was literally the characters x and x... but it would yield the same result for any strings repeated amongst each other... let's say compare "abcd" with "jklm" will also be true, since the pattern of letters is the same.
    – Juan Lopez
    Commented Apr 12, 2018 at 13:50
  • B can have at most the same as A, the limit is 10^7 characters. Permutations of "xyz" could be "abc" "bcd" "bca" etc...
    – Juan Lopez
    Commented Apr 14, 2018 at 20:02

2 Answers 2

0

Concept: Use Regular Expressions

You would need the following regular expression (\\w)\\1{(REPETITIONS-1)}
I don't know about C but Java provides a library to compile RegEx patterns. Here's a class that implements just what you want:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringPatternPermutation {
    public static void main(String[] args) {
        int REPETITIONS = 3;
        String REGEX = "(\\w)\\1{" + (REPETITIONS-1) + "}";
        String INPUT = "abbbbbbccddeffff";

        Pattern p = Pattern.compile(REGEX);
        Matcher m = p.matcher(INPUT); 

        int count = 0;
        while(m.find()){
            String match = m.group();
            System.out.println(match);
            count++;
        }

        System.out.println(count);
    }
}

Here's a test of the code above: https://ideone.com/5nztaa
Here's a useful website to test any RegEx: https://regexr.com/

2
  • I'd use anything else but they ask me specifically for C :(
    – Juan Lopez
    Commented Apr 12, 2018 at 13:50
  • There must be some way to work with regex in C. Check GNU C Library
    – Ahmad Tn
    Commented Apr 12, 2018 at 17:07
-1

Without Regular Expressions

public class StringPatternPermutation {

    public static void main(String[] args) {
        String a = "abjjjiixsssppw";
        String b = "qwwwee";

        String patternA = detectPattern(a);
        String patternB = detectPattern(b);

        System.out.println("-String A: " + a);
        System.out.println("-Pattern A: " + patternA);
        System.out.println("-String B: " + b);
        System.out.println("-Pattern B: " + patternB);
        System.out.println("-A contains B? " + patternA.contains(patternB));

        int count = 0;
        int index = 0;      
        while((index = patternA.indexOf(patternB)) != -1){
            count++;
            patternA = patternA.substring(index+1, patternA.length());
        }

        System.out.println("-Number of occurances: " + count);
    }

    private static String detectPattern(String a){
        StringBuilder sb = new StringBuilder();
        char prev = a.charAt(0);
        int count = 1;

        for(int i = 1; i < a.length(); i++){            
            char curr = a.charAt(i);
            if(curr == prev)
                count++;
            else {
                sb.append(count + ", ");
                prev = curr;
                count = 1;
            }

            if(i == a.length() - 1){
                sb.append(count);
            }
        }
        return sb.toString();
    }

}


Test it on ideOne: https://ideone.com/w422Du

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.