Skip to main content

Command Palette

Search for a command to run...

Uncommon Words from Two Sentences

Published
2 min read
Uncommon Words from Two Sentences
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.

A sentence is a string of single-space separated words where each word consists only of lowercase letters.

A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence.

Given two sentences s1 and s2, return a list of all the uncommon words. You may return the answer in any order.

LeetCode Problem - 884

class Solution {
    public String[] uncommonFromSentences(String s1, String s2) {
        // Split the two input strings into arrays of words
        String[] s1Arr = s1.split(" ");
        String[] s2Arr = s2.split(" ");

        // Calculate the total length of the combined array
        int len = s1Arr.length + s2Arr.length;

        // Create a new array to store all words from both sentences
        String[] s3Arr = new String[len];

        // Copy the words from s1Arr into the combined array
        System.arraycopy(s1Arr, 0, s3Arr, 0, s1Arr.length);

        // Copy the words from s2Arr into the combined array starting from where s1Arr ends
        System.arraycopy(s2Arr, 0, s3Arr, s1Arr.length, s2Arr.length);

        // Create a HashMap to store the frequency of each word
        Map<String, Integer> mp = new HashMap<>();

        // Loop through the combined array and count the occurrences of each word
        for (String s : s3Arr) {
            mp.put(s, mp.getOrDefault(s, 0) + 1);
        }

        // Create a list to store the uncommon words
        List<String> al = new ArrayList<>();

        // Loop through the combined array again and check if any word appears exactly once
        for (String s : s3Arr) {
            if (mp.get(s) == 1) {
                al.add(s);  // If the word appears only once, add it to the list
            }
        }

        // Convert the list of uncommon words into an array
        String[] resultArr = new String[al.size()];
        for (int i = 0; i < al.size();

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.