Hewlett Packard Interview Question
Software Engineer / DevelopersTeam: Networking
Country: United States
Interview Type: In-Person
public static int stepsToMakePalindrome(String word) {
System.out.println("String received : "+word);
char[] characters = word.toCharArray();
int i = 0;
int j = characters.length - 1;
int steps = 0;
while (i != j) {
if (characters[i] != characters[j]) {
steps++;
if (characters[j] > characters[i]) {
characters[j] = characters[i];
} else {
characters[i] = characters[j];
}
}
i++;
j--;
}
System.out.println("String transformed into Palindrome "+String.copyValueOf(characters));
return steps;
}
public static int stepsToMakePalindrome(String word) {
System.out.println("String received : "+word);
char[] characters = word.toCharArray();
int i = 0;
int j = characters.length - 1;
int steps = 0;
while (i != j) {
if (characters[i] != characters[j]) {
steps++;
if (characters[j] > characters[i]) {
characters[j] = characters[i];
} else {
characters[i] = characters[j];
}
}
i++;
j--;
}
System.out.println("String transformed into Palindrome "+String.copyValueOf(characters));
return steps;
}
public static int stepsToMakePalindrome(String word) {
System.out.println("String received : "+word);
char[] characters = word.toCharArray();
int i = 0;
int j = characters.length - 1;
int steps = 0;
while (i != j) {
if (characters[i] != characters[j]) {
steps++;
if (characters[j] > characters[i]) {
characters[j] = characters[i];
} else {
characters[i] = characters[j];
}
}
i++;
j--;
}
System.out.println("String transformed into Palindrome "+String.copyValueOf(characters));
return steps;
}
public static int stepsToMakePalindrome(String word) {
System.out.println("String received : "+word);
char[] characters = word.toCharArray();
int i = 0;
int j = characters.length - 1;
int steps = 0;
while (i != j) {
if (characters[i] != characters[j]) {
steps++;
if (characters[j] > characters[i]) {
characters[j] = characters[i];
} else {
characters[i] = characters[j];
}
}
i++;
j--;
}
System.out.println("String transformed into Palindrome "+String.copyValueOf(characters));
return steps;
}
public static int stepsToMakePalindrome(String word) {
System.out.println("String received : "+word);
char[] characters = word.toCharArray();
int i = 0;
int j = characters.length - 1;
int steps = 0;
while (i != j) {
if (characters[i] != characters[j]) {
steps++;
if (characters[j] > characters[i]) {
characters[j] = characters[i];
} else {
characters[i] = characters[j];
}
}
i++;
j--;
}
System.out.println("String transformed into Palindrome "+String.copyValueOf(characters));
return steps;
}
Count the number of differences when taking the mirror image. C solution:
- JonathanBarOr November 01, 2015