VB
BAN USER- -1of 5 votes
AnswersGoldman's conjecture - already posted,
- VB in United States
Well ordered numbers - already posted.| Report Duplicate | Flag | PURGE
Epic Systems Software Engineer / Developer Algorithm - 1of 1 vote
AnswersThe cows and bulls game, Player A chooses a word and player B guesses a word. You say bulls when a character in the player B's guess match with a character in player A's word and also it is in the corect position as in A's word. You say cows, when a character in the player B's word match the character in player A, but it is not in the correct position. The characters are case insensitive. Given two words player A's and player B's,Write a function that return the number of bulls and no of cows. For example,
- VB in United States
A - Picture B- Epic, bulls -0, cows - 4
A - forum B - four, bulls - 3 cows - 1| Report Duplicate | Flag | PURGE
Epic Systems Software Engineer / Developer Algorithm - 0of 2 votes
AnswersJumper Game: A NxN grid which contains either of 0-empty, 1 - player1, 2 - player 2. Given a position in the grid, find the longest jump path. For jump path, you can horizontally or vertically, you can jump on opponent cell and also the landing cell should be empty. No opponent cell can be jumped more than once. Write a function which takes grid and a specific position in the grid, and returns the longest possible number of jumps in the grid.
- VB in United States| Report Duplicate | Flag | PURGE
Epic Systems Software Engineer / Developer Algorithm
My solution. tested and it handles lower case and upper case combinations of well ordered strings
private static void getWellOrderedString(String currentString,int charsLeft)
{
recursion_count++;
if(charsLeft==0)
{
System.out.println(currentString);
stringCount++;
}
char i, j;
if(currentString.isEmpty()) {
i='a';
j = 'A';
}
else {
char lastchar = currentString.charAt(currentString.length()- 1);
if(lastchar >= 'a' && lastchar < 'z') {
i=(char) (lastchar + 1);
j = (char) (lastchar - 32 + 1) ;
}
else if(lastchar >= 'A' && lastchar < 'Z') {
j = (char) (lastchar + 1);
i = (char) (lastchar + 32 + 1);
}
else {
return;
}
}
for (;i <='z' && charsLeft > 0; i++)
{
getWellOrderedString(currentString+i, charsLeft-1);
}
for (;j <='Z' && charsLeft > 0; j++)
{
getWellOrderedString(currentString+j, charsLeft-1);
}
}
I think we need to consider the invalid input like actual = "1246" expected = "12646". We can check this by adding this condition
if(actual.charAt(i) != expected.charAt(j)) {
if(fault == '\0')
fault = expected.charAt(j);
else {
if(expected.charAt(j) != fault) return false;
}
--i;
}
// Extra check added
else{
if(fault != '\0' && actual.charAt(i) == fault) return false;
}
++i;
My solution. complied and tested. Scope for improvement.
public class StringReplace {
static char vowels [] = {'a', 'e', 'i', 'o', 'u'};
static char replacements [] = {'A', 'E', 'I', 'O', 'U'};
public static void replaceChars(String input) {
int i, j, third_vowel_pos = 0;
int n = input.length();
int count = 0;
String output = "";
boolean isVowel = false;
for(i = 0; i < n; ++i) {
isVowel = false;
for(j =0; j < vowels.length;++j) {
if(input.charAt(i) == vowels[j]) {
count++;
isVowel = true;
break;
}
}
output += input.charAt(i);
if(count == 3) {
third_vowel_pos = i;
break;
}
}
count = 0;
String temp = "";
for(i = n - 1; i > third_vowel_pos; --i) {
isVowel = false;
for(j =0; j < vowels.length && count < 4;++j) {
if(input.charAt(i) == vowels[j]) {
isVowel = true;
count++;
break;
}
}
if(isVowel) {
temp += replacements[j];
}
else{
temp += input.charAt(i);
}
}
for(i = temp.length() - 1; i >= 0; --i)
output += temp.charAt(i);
System.out.println(output);
}
public static void main(String args[]) {
String input = "ac1;fheojkuvakioka";
String input2 = "aei";
String input3 = "aeiouia";
replaceChars(input);
replaceChars(input2);
replaceChars(input3);
}
}
My solution. first + second = rest of the number.Can handle different lengths in rest of the number
public class Increasing_Number {
public static boolean checkNumber(int number) {
String num = number + "";
int l, j, length;
int n = num.length();
int first, second, rest;
// length of number to check
length = n / 2;
// Checking all possible lengths
for(l = 1; l <= length; ++l) {
// get all substrings
for(j = 0; j <= n - 3 * l; ++j) {
first = Integer.parseInt(num.substring(j, j+l));
second = Integer.parseInt(num.substring(j + l, j + l * 2));
// Rest of the substring is iterated for all lengths
for(int i = j + 2*l + 1; i <= n ; ++i) {
rest = Integer.parseInt(num.substring(j + 2* l, i));
if((first + second) == rest) {
if(i == n) {
return true;
}
}
}
}
}
return false;
}
public static void main(String args[]) {
int start = 101020;
int end = 101020;
boolean result = false;
for(int i = start; i <= end; ) {
result = checkNumber(i);
System.out.println(i + " : " + result);
// i = i * 10 - 18;
}
}
}
Readable and tested code for four adjacent cells. You can extend it to 8 also easily.
- VB November 09, 2014