David has several containers, each with a number of balls in it. He has just enough containers to sort each type of ball he has into its own container. David wants to sort the balls using his sort method. As an example, David has containers and different types of balls, both of which are numbered from to . The distribution of ball types per container are described by an matrix of integers, . For example, consider the following diagram for : In a single operation, David can swap two balls located in different containers. The diagram below depicts a single swap operation: David wants to perform some number of swap operations such that: Each container contains only balls of the same type. No two balls of the same type are located in different containers. You must perform queries where each query is in the form of a matrix, . For each query, print Possible on a new line if Davi...
Given a String S, reverse the string without reversing its individual words. Words are separated by dots. Example 1: Input: S = i.like.this.program.very.much Output: much.very.program.this.like.i Explanation: After reversing the whole string(not individual words), the input string becomes much.very.program.this.like.i Example 2: Input: S = pqr.mno Output: mno.pqr Explanation: After reversing the whole string , the input string becomes mno.pqr Your Task: You dont need to read input or print anything. Complete the function reverseWords() which takes string S as input parameter and returns a string containing the words in reversed order. Each word in the returning string should also be separated by '.' Expected Time Complexity: O(|S|) Expected Auxiliary Space: O(|S|) Constraints: 1 <= |S| <= 2000 Solution: Java
Explanation: Find the index of the element in the array and then take the index value to find it's index. Eg: Array 2 3 1 Index of 1 is 3 Find index of 3 which is 2 Add to new array Index of 2 is 1 Find index of 1 which is 3 Add to new array Repeat Steps def permutationEquation ( p ): new = [] for i in range ( 1 , len ( p ) + 1 ): new . append ( p . index ( p . index ( i ) + 1 ) + 1 ) return new
Comments
Post a Comment