CDW04-CODEWARS

Practice
Contest link

Author: Akshat Agarwal
Tester: Amit Bhardwaj
Editorial: Adarsh Kumar Srivastava

Difficulty:
Easy

Prerequisite:
Array ,hashing

Explanation & Algorithm:
In the question, you have given const initial string “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890”

and N queries, each query contain K strings you have to find the position of the character in the initial constant string and a given string, sum them

Example String “BY”

in the string “BY” its position in the constant string is (1,24) and in the given string is (0,1)

The sum is (0+1,1+24) =(1,25)

Then sum all characters and then multiple it with k.

To find the position of the character in the constant string we use find function present in C++/ indexof function present in Java.

or you can also use unordered_map in C++ / Hashtable in Java (to store all constant characters with their index ).

Solution

Setter's Code

# include < bits/stdc++.h >

using namespace std ;

# define int long long

void solve ( string p )

{

int n ;

cin >> n ;

int n2 = n ;

string s ;

int ans = 0 ;

while ( n2 ){

cin >> s ;

int sum = 0 ;

for ( int i = 0 ; i < s . size (); i ++ )

{

sum += i + p . find ( s [ i ] );

}

ans += sum ;

}

cout << ans ***** n << " \n ";

}

signed main ()

{

ios_base :: sync_with_stdio ( false );

cin . tie ( 0 ); cout . tie ( 0 );

int test = 1 ;

cin >> test ;

while ( test )

{

string p ;

getline ( cin , p );

p = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 ";

solve ( p );

}

return 0 ;

}

Tester's Code

t = int(input())
slst = []
ts = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890”
for i in range(0,t):
N = int(input())
lst = input().split(’ ')
sum = 0
for j in range(0,N):
s = lst[j]
for k in range(0, len(s)):
sum += k + ts.find(s[k])
slst.append(sum*N)
for i in range(0,t):
print(slst[i])