dimanche 19 avril 2015

max no of times a particular character in a string in knuth morris pratt algorithm comes into comparison with the string?

Let



T:String
P:pattern


what is the max no of times a particular character in a string(T) in knuth morris pratt algorithm comes into comparison with the pattern(P) ?


PHP check if string contains maximum 30 letters/numbers in a sequence with preg_match

I'm stuck here and I can't find any results for my question, maybe because english is not my native language.


I want to match lines which contain maximum 30 letters/numbers in a sequence:


Is this even possible with preg_match?



preg_match("/[^A-Za-z0-9](max 30 in a sequence)/", $string)


Strings:



$string = "1234567890123456789012345678901234567890"; // FALSE
$string = "sdfihsgbfsadiousdghiug"; // TRUE
$string = "cfgvsdfsdf786sdf78s9d8g7stdg87stdg78tsd7g0tsd9g7t"; // FALSE
$string = "65656.sdfsdf.sdfsdf"; // TRUE
$string = "ewrwet_t876534875634875687te8---7r9w358wt3587tw3587"; // TRUE
$string = "sd879dtg87dftg87dftg87ftg87tfg087tfgtdf8g7tdf87gt8t___454"; // FALSE

How to put a string into a byte array in x86 assembly?

How do you get a string from stdin and but it in a byte array using sys_read in x86 assembly, NASM? I am having trouble accessing the different indices of the array.


Find the first index of a character in a string

I am supposed to loop through the character of arrays that is passed in and look for the first occurrence of char, then return the index of the first occurrence. if char is not found then I return -1. This seems to work for all characters except the character at 0, which it does not find for some reason.



int find_ch_index(char string[], char ch) {
int i = 0;
while (string[i++]) {
if (string[i] == ch) {
return i;
}
}
return -1;
}

Timepicker Integer Error

I have a timepicker in my preference activity for setting the time when a notification should be displayed. The value is stored as a string, for example: "15:45". To understand the problem, I will further explain what happens next to the value:



SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(context);
String hour = pref.getString("notification_time","");
// notification_time is my preference key
String Hora = hour;
int hours = Integer.parseInt(Hora.substring(0, 2));
int min = Integer.parseInt(Hora.substring(3, 5));
// as you can see, I parse the string, and then use the integers to set the time (see below)
calendar.set(Calendar.HOUR_OF_DAY, hours);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 00);


Now the problem is, My TimePicker stores the value differently, if the time is AM: for example, if you set the time to 07:45, it stores the time in the string as "7:45", not "07:45", and thus this line in the code fails:



int hours = Integer.parseInt(Hora.substring(0, 2));


(Throwing this error, not really necessary to understand the problem):



java.lang.NumberFormatException: Invalid int: "5:"


,because the position for "substring" isnt working anymore. (1 digit stored in the string instead of 2). Same goes for minutes, for example if I set the minutes to 08, my timepicker stores them as 8, and the same problem occurs again.


Now I have thought about two ways to solve this problem: Either I change the code in my settingsactivity and parse the string differently, or I change the way how I store the strings:



if (positiveResult) {
lastHour=picker.getCurrentHour();
lastMinute=picker.getCurrentMinute();
String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute);

if (callChangeListener(time)) {
persistString(time);
}
setSummary(getSummary());
}


(These are the lines of code responsible for saving the value as a string)


How should I solve the problem?


How can I get Java to check a String for matches to a Pattern starting at the end?

I have a program that is reading lines of input from a file and must extract information from from each line using a regular expression. Each line is the absolute path to a file or folder in a file system that my application creates. Each line has the following format:



/root/folder1/folder2/folder3


What I need to do is separate the String into an array containing the first part of the path and then the last folder as follows:



["/root/folder1/folder2", "folder3"]


My idea of how to do this was to use a regular expression in conjunction with the java.util.regex.Pattern#split(CharSequence, int) method to split the string using the Pattern while limiting the size of the resultant array. However, since this method matches the pattern to the string starting at the beginning, this method won't work for me. I need something with similar functionality that would check the String for matches starting at the end rather that the beginning. Either that or I need a regular expression wizard to help me cook up a new regular expression to accomplish this.


Right now, I'm using the simple regex "[/]" to split the string.


Thanks in advance.


Using pairs of words from a dictionary with no letters in common, find a pair that maximizes the sum of the words' lengths

Question:



Using pairs of words from a dictionary with no letters in common, find a pair that maximizes the sum of the words' lengths


Example Dictionary: mouse, cow, join, key, dog


dog and key share no letters and have a sum of 3+3 = 6


mouse does not work with cow, join, or dog because they all share the letter 'o'


join and key share no letters and have a sum of 4+3 = 7



I had this question in an interview, my solution I came up with is outlined below. I was wondering if there is any way to make it more efficient? I used two BitSets to map the alphabet of two words and AND them together to see if they contain the same letters. I think my algorithm has a complexity is o(n!) which is inefficent, is there a better way to optimize my algorithm?



public static void maximumSum (String[] dictionary) {
// ascii of a = 97
BitSet word1 = new BitSet(26);
BitSet word2 = new BitSet(26);

String maxWord1 = "";
String maxWord2 = "";
int maxSum = -1;

for(int i = 0; i<dictionary.length; i++) {
for(int j = i+1; j<dictionary.length; j++) {
String s1 = dictionary[i];
String s2 = dictionary[j];
for(int k = 0; k<s1.length(); k++) {
word1.set(s1.charAt(k)-97);
}
for(int k = 0; k<s2.length(); k++) {
word2.set(s2.charAt(k)-97);
}
word1.and(word2);
if(word1.cardinality() == 0) {
if(maxSum < s1.length()+s2.length()) {
maxWord1 = s1;
maxWord2 = s2;
maxSum = s1.length()+s2.length();
}
}
word1.clear();
word2.clear();
}
}
if(maxSum == -1)
System.out.println("All the words have letters in common.");
else
System.out.println("'"+maxWord1+"' and '"+maxWord2+"'
have a maximum sum of "+maxSum);
}

public static void main(String[] args) {
String[] dictionary = {"mouse", "cow", "join", "key", "dog"};
maximumSum(dictionary);
}


output:



'join' and 'key' have a maximum sum of 7