Skip to main content

Command Palette

Search for a command to run...

Permutation Difference between Two Strings

Published
1 min read
Permutation Difference between Two Strings
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given two strings s and t such that every character occurs at most once in s and t is a permutation of s.

The permutation difference between s and t is defined as the sum of the absolute difference between the index of the occurrence of each character in s and the index of the occurrence of the same character in t.

Return the permutation difference between s and t.

LeetCode Problem - 3146

class Solution {

    // Method to find the sum of absolute differences in positions of each character in s and t
    public int findPermutationDifference(String s, String t) {
        int absoluteDifference = 0; // Initialize the total absolute difference

        // Iterate through each character in the string s
        for(int i = 0; i < s.length(); i++){
            // Find the matching character in string t
            for(int j = 0; j < t.length(); j++){
                if(s.charAt(i) == t.charAt(j)){
                    // Calculate the absolute difference in positions and add to the total
                    absoluteDifference += Math.abs(i - j);
                    break; // Break the inner loop once the character is found
                }
            }
        }
        // Return the total absolute difference
        return absoluteDifference;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.