The Love-Letter Mystery
Problem Statement :
James found a love letter that his friend Harry has written to his girlfriend. James is a prankster, so he decides to meddle with the letter. He changes all the words in the letter into palindromes. To do this, he follows two rules: 1. He can only reduce the value of a letter by 1, i.e. he can change d to c, but he cannot change c to d or d to b. 2. The letter a may not be reduced any further. Each reduction in the value of any letter is counted as a single operation. Find the minimum number of operations required to convert a given string into a palindrome. Function Description Complete the theLoveLetterMystery function in the editor below. theLoveLetterMystery has the following parameter(s): string s: the text of the letter Returns int: the minimum number of operations Input Format The first line contains an integer q, the number of queries. The next q lines will each contain a string s. Constraints 1 <= q <= 10 1 <= | s | <= 10^4 All strings are composed of lower case English letters, ascii[a-z], with no spaces.
Solution :
Solution in C :
In C++ :
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
string s;
cin >> s;
int i = 0;
int j = s.length()-1;
int sol = 0;
while(i<j){
sol += abs(s[i]-s[j]);
++i;
--j;
}
cout<<sol<<"\n";
}
return 0;
}
In Java :
import java.util.Scanner;
public class Solution {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();scan.nextLine();
for(int i=0;i<T;i++)
{
String s = scan.nextLine();
int count=0;
for(int j=0;j<s.length()/2;j++)
count+=Math.abs(s.charAt(j)-s.charAt(s.length()-1-j));
System.out.println(count);
}
}
}
In C :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int T,n,ans,i;
char s[20000];
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%s",&s);
n=strlen(s);
for(ans=i=0;i<n-1-i;i++)
ans+=abs(s[i]-s[n-1-i]);
printf("%d\n",ans);
}
return 0;
}
In Python3 :
def case(t):
alphabet = "abcdefghijklmnopqrstuvwxyz"
rev = t[::-1]
ans = 0
for i in range(len(t) // 2):
op1 = t[i]
op2 = rev[i]
pos1 = alphabet.find(op1)
pos2 = alphabet.find(op2)
ans += abs(pos1 - pos2)
return ans
t = int(input())
for i in range(t):
s = input()
print(case(s))
View More Similar Problems
Jesse and Cookies
Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with: sweetness Least sweet cookie 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a sweetness > = K. You are given Jesse's cookies. Print t
View Solution →Find the Running Median
The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.
View Solution →Minimum Average Waiting Time
Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h
View Solution →Merging Communities
People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w
View Solution →Components in a graph
There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu
View Solution →Kundu and Tree
Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that
View Solution →