[go: up one dir, main page]

0% found this document useful (0 votes)
3 views7 pages

String Q28

The document presents various programming problems along with their solutions, including finding the most frequent character, the first non-repeating character, checking if two strings are anagrams, finding the longest word in a sentence, and checking for unique characters in a string. Each problem is accompanied by example inputs and outputs, as well as explanations of the solutions. Additionally, it offers resources for interview preparation and information about courses available for skill enhancement.

Uploaded by

Vignesh Shenoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

String Q28

The document presents various programming problems along with their solutions, including finding the most frequent character, the first non-repeating character, checking if two strings are anagrams, finding the longest word in a sentence, and checking for unique characters in a string. Each problem is accompanied by example inputs and outputs, as well as explanations of the solutions. Additionally, it offers resources for interview preparation and information about courses available for skill enhancement.

Uploaded by

Vignesh Shenoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Find the most frequent character

Problem: You are given a string. Write a program to find the most frequently occurring
character in the string. If multiple characters have the same highest frequency, return the first
one encountered.
Input: "javascript"
Output: "a"
(Explanation: The letter "a" appears twice, which is the highest frequency.)
Input: "apple"
Output: "p"
(Explanation: The letter "p" appears twice, which is the most frequent
character.)
Input: "hello world"
Output: "l"
(Explanation: The letter "l" appears three times, which is the highest
frequency.)
Input: "abcd"
Output: "a"
(Explanation: All characters appear only once, so we return the first
character encountered.)

Solution:
function mostFrequentCharacter(str) {
let frequency = {}; // Object to store character counts
let maxChar = ""; // Stores the character with the highest frequency
let maxCount = 0; // Stores the highest frequency count

for (let char of str) {


if (char !== " ") {
// Ignore spaces
frequency[char] = (frequency[char] || 0) + 1; // Count each character

// Update maxChar if the current character has a higher frequency


if (frequency[char] > maxCount) {
maxCount = frequency[char];
maxChar = char;
}
}
}

return maxChar;
}

// Test cases
console.log(mostFrequentCharacter("javascript")); // Output: "a"
console.log(mostFrequentCharacter("apple")); // Output: "p"
console.log(mostFrequentCharacter("hello world")); // Output: "l"
console.log(mostFrequentCharacter("abcd")); // Output: "a"
Explanation:

1. Create an empty object frequency to store character counts.


2. Loop through the string, counting occurrences of each character.
o If a character appears more times than maxCount, update maxCount and
maxChar.
3. Ignore spaces (" ") to focus only on letters.
4. Return the character with the highest frequency.
o If multiple characters have the same count, the first encountered is returned.

O(n) → We iterate through the string once to count characters and once to find the max.

Find the First Non-Repeating Character

Problem: You are given a string. Write a program to find the first non-repeating character in
the string. If all characters repeat, return null.

Examples:

Input:
"javascript"
Output:
"j"
(Explanation: "j" appears only once, and it's the first unique character.)
Input:
"apple"
Output:
"a"
(Explanation: "a" appears once before other unique characters.)
Input:
"aabbcc"`
Output:
null
(Explanation: All characters repeat, so we return null.)

Solution:

function firstNonRepeatingCharacter(str) {
let frequency = {};

// Count occurrences of each character


for (let char of str) {
frequency[char] = (frequency[char] || 0) + 1;
}

// Find the first non-repeating character


for (let char of str) {
if (frequency[char] === 1) {
return char;
}
}

return null; // If all characters repeat


}

// Test cases
console.log(firstNonRepeatingCharacter("javascript")); // Output: "j"
console.log(firstNonRepeatingCharacter("apple")); // Output: "a"
console.log(firstNonRepeatingCharacter("aabbcc")); // Output: null

Explanation:

1. Create a frequency object to count occurrences of each character.


2. Iterate through the string again and return the first character that appears only once.
3. If no unique character exists, return null.

Check If Two Strings Are Anagrams

Problem: You are given two strings. Write a program to check if they are anagrams (contain
the same letters in a different order).

Examples:

Input:
"listen", "silent"
Output:
true
(Explanation: Both words contain the same letters, rearranged.)
Input:
"hello", "world"
Output:
false
(Explanation: Different letters, so not anagrams.)

Solution:

function areAnagrams(str1, str2) {


// Sort both strings and compare
return str1.split("").sort().join("") === str2.split("").sort().join("");
}

// Test cases
console.log(areAnagrams("listen", "silent")); // Output: true
console.log(areAnagrams("hello", "world")); // Output: false
console.log(areAnagrams("triangle", "integral")); // Output: true
Explanation:

1. Convert both strings into arrays using .split("").


2. Sort the arrays using .sort().
3. Join the sorted arrays back into strings using .join("") and compare them.

Find the Longest Word in a Sentence

Problem: You are given a sentence as a string. Write a program to find the longest word in
the sentence.

Examples:

Input:
"The quick brown fox jumps"
Output:
"jumps"
(Explanation: "jumps" is the longest word with 5 letters.)
Input:
"I love JavaScript"
Output:
"JavaScript"
(Explanation: "JavaScript" is the longest word with 10 letters.)

Solution:

function longestWord(sentence) {
let words = sentence.split(" ");
let longest = "";

for (let word of words) {


if (word.length > longest.length) {
longest = word;
}
}

return longest;
}

// Test cases
console.log(longestWord("The quick brown fox jumps")); // Output: "jumps"
console.log(longestWord("I love JavaScript")); // Output: "JavaScript"
console.log(longestWord("Coding is fun")); // Output: "Coding"

Explanation:
1. Split the sentence into an array of words.
2. Iterate through each word, keeping track of the longest one.
3. Return the longest word found.

Check If a String Contains Only Unique Characters

Problem: Write a program to check if a given string contains only unique characters.

Examples:

Input:
"abcdef"
Output:
true
(Explanation: All characters are unique.)
Input:
"hello"
Output:
false
(Explanation: "l" appears twice.)
Input:
"world"
Output:
true
(Explanation: All characters are unique.)

Solution:

function hasUniqueCharacters(str) {
let seen = new Set();

for (let char of str) {


if (seen.has(char)) {
return false; // If character is repeated, return false
}
seen.add(char);
}

return true;
}

// Test cases
console.log(hasUniqueCharacters("abcdef")); // Output: true
console.log(hasUniqueCharacters("hello")); // Output: false
console.log(hasUniqueCharacters("world")); // Output: true
Explanation:

1. Use a Set to track characters seen before.


2. Iterate through the string:
o If a character is already in the Set, return false.
o Otherwise, add it to the Set.
3. If no duplicates are found, return true.

For More Interview Questions:


Looking to ace your next opportunity? Prepare with our expert-curated resources!

Get Your PDF Files Instantly:

Structured learning made simple! Choose from our affordable guides:

1. Frontend Development PDF = ₹99/-


Covers ReactJS, and JavaScript.
2. MERN Stack Development PDF = ₹149/-
Comprehensive guide for Frontend, Backend, and Database development.
3. Backend Development PDF = ₹59/-
Focused on Node.js, Express.js, MongoDB, and related technologies.

Coming Soon (02.02.2025 @6:30 pm): 500+ Best Questions from LeetCode and
HackerRank
Carefully selected questions frequently asked by top companies to help you ace your
interviews!
How to Get It?

• Message us on WhatsApp: 9398123134


• Pay the respective amount, and receive your PDF instantly!

Instant replies are available between 8:00 AM to 10:00 PM IST.

Join Our Online and Offline Classes

Upskill with confidence—our courses come with a


100% PLACEMENT GUARANTEE!

1. Frontend Technologies
Learn ReactJS, Angular, and modern UI/UX design.
2. Full Stack Development
Master MERN stack, Java, Python, or .NET technologies.
3. Data Science and Analytics
Gain skills in data visualization, modeling, and analysis.
4. Artificial Intelligence and Machine Learning
Explore AI tools, concepts, and practical implementations.
5. Advanced Topics
o Cloud Computing: AWS, Azure
o Cybersecurity: Ethical hacking, penetration testing

How to Enroll?

Call us at 9398123134 to get started today!

You might also like